Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

create = False

#73

Submitted by: yurj@alfa.it

def getconn(self, create=True):

    conn = pool.getconn(self.dsn)
    conn.set_isolation_level(int(self.tilevel))
    conn.set_client_encoding(self.encoding)
    for tc in self.typecasts:
        register_type(tc, conn)
    return conn

where's the test if create = False. With this code it is impossible to roll back with Zope.

Reported by Psycopg website · October 25th, 2011 @ 12:18 PM

State: resolved
Milestone: none
Assigned to: nobody

Activity

  1. yuri
    yuri

    def getconn(self, create=True):

        conn = pool.getconn(self.dsn)
        if create == False:
         return conn
        conn.set_isolation_level(int(self.tilevel))
        conn.set_client_encoding(self.encoding)
        for tc in self.typecasts:
            register_type(tc, conn)
        return conn
    

    solve the problem.

    Or am I totally wrong? :)

    October 25th, 2011 @ 12:26 PM

  2. Federico Di Gregorio
    Federico Di Gregorio

    "create" isn't used at all in that method. What do you mean by "impossible to roll back with Zope". Can you make an example?

    October 25th, 2011 @ 12:30 PM

  3. yuri
    yuri

    install a fresh zope
    create an adapter instance
    go in "test"
    write a wrong query

    Then, you cannot submit other queries bacause of "current transaction is aborted, commands ignored until end of transaction block" feature.

    I've to restart postgres or zope to submit (only correct) queries.

    adding:

    if create == False:
     return conn
    

    solve the problem. This is because _abort requires the connection and call getconn with create=False. But there's no check in it, so conn.set_isolation_level(int(self.tilevel)) gives an exception because no rollback has been yet submitted.

    October 25th, 2011 @ 01:19 PM

  4. Daniele Varrazzo
    Daniele Varrazzo

    yuri,

    could you test the behaviour with the current head version (e.g. you may check it out from the devel branch in https://github.com/dvarrazzo/psycopg/).

    The problem seems created by a bad connection in the pool. I've added a check to rollback connections in transaction and discard broken ones when returned to the pool, so the issue may have already been fixed.

    Thank you.

    November 3rd, 2011 @ 03:15 PM

  5. yuri
    yuri

    Thank you for the fix. I cannot see it, I mean I can understand what has changes. Also github seems to be at 2.4.1 version, and not 2.4.2... I'm not very good with github, can you explain me how I can see the change and how to download the correct version? :)

    November 3rd, 2011 @ 03:47 PM

  6. yuri
    yuri

    "I cannot see it, I mean I can understand what has changes"

    I can't understand, sorry for the mistake.

    November 3rd, 2011 @ 03:48 PM

  7. Daniele Varrazzo
    Daniele Varrazzo

    No problem:

    git clone git://github.com/dvarrazzo/psycopg.git
    cd psycopg
    git co devel  (it may say you are already on branch devel)
    make
    

    you should have now a compiled version of psycopg in build/lib.2.6 (or whatever you python version is).

    Cheers!

    November 3rd, 2011 @ 03:56 PM

  8. Daniele Varrazzo
    Daniele Varrazzo

    Sorry: "git co" should be "git checkout". "co" is an alias of mine.

    November 3rd, 2011 @ 03:56 PM

  9. yuri
    yuri

    Hi!

    I've installed it, replaced the path in easyinstall.pth (I don't know if it is important but I did it), modified DA-py to get 2.4.3.dev0 version.

    But the error is always the same, I can't see any change. Can you show me the change? I think the fix above is needed, it has not sense to create a new connection when you abort a transaction. But I'm not aware of how it works so I shut up :)

    November 4th, 2011 @ 07:22 AM

  10. yuri
    yuri

    make just compiled it, I've run python setup.py build and then install, using the zope python. All seems correct to me.

    November 4th, 2011 @ 07:25 AM

  11. Daniele Varrazzo
    Daniele Varrazzo

    But the error is always the same, I can't see any change. Can you show me the change?

    The change is this: https://github.com/dvarrazzo/psycopg/commit/a2ee25ecfe600a3d0c5d1ea...

    I think the fix above is needed, it has not sense to create a new connection when you abort a transaction.

    This is not what the patch does. The patch rolls back the connection if it is in transaction/in error, and discards it if broken.

    make just compiled it, I've run python setup.py build and then install, using the zope python.

    Yeah, could do.

    Sorry, I can't help you further: I don't do Zope.

    November 4th, 2011 @ 09:09 AM

  12. yuri
    yuri

    The problem is in the ZPsycopgDA adapter. Who is responsible of that code?

    I think the fix above is needed, it has not sense to create a new connection when you abort a transaction. This is not what the patch does. The patch rolls back the connection if it is in transaction/in error, and discards it if broken.

    Yes, but the ZPsycopgDA adapter does this:

    def _abort(self, *ignored):
        try:
            conn = self.getconn(False) <-- here It calls getconn
            conn.rollback()
            self.putconn()
        except AttributeError:
            pass
    

    and getconn(False):

    def getconn(self, create=True):
        conn = pool.getconn(self.dsn)
        conn.set_isolation_level(int(self.tilevel)) <---- here the problem
    

    set_isolation_level writes on the connection BEFORE the roolback done in conn.rollback().

    getconn() should honour the False case, just returning the connection and not calling set_isolation_level because it writes on an unrolled postgres error and this gives and error and the rollback/abort is not performed.

    I propose just to add the if create == False check:

    def getconn(self, create=True):

      conn = pool.getconn(self.dsn)
      if create == False:
       return conn
      ...
    

    in the ZPsycopgDA/db.py code.

    November 4th, 2011 @ 09:29 AM

  13. Federico Di Gregorio
    Federico Di Gregorio

    Yuri, you're right and your patch is OK. I'll include it for 2.4.3.

    November 4th, 2011 @ 09:37 AM

  14. yuri
  15. Daniele Varrazzo
    Daniele Varrazzo
    • State changed from new to resolved

    Fix released in 2.4.4

    December 19th, 2011 @ 12:53 PM