[ZODB-Dev] ZODB 3.2.4 ConnectionStateError

Syver Enstad syver at inout.no
Thu Nov 4 06:09:24 EST 2004


"Tim Peters" <tim at zope.com> writes:

> > You're never *not* "in" a transaction.  The instant abort() returns, a new
> > transaction has implicitly begun.  So, yes, it's safe to access objects
> > after aborting, but you're technically in a new transaction then.  If you
> > modify any of these objects, you'll again need to abort or commit the
> > transaction before closing the connection.

The problem here arises when one does something like this:

connection = db.open()
try:
    for each in connection.root()['mycollection']:
        each.setSomeState(arg)
    connection.getTransaction().commit()
finally:
    connection.close()

If each.setSomeState raises for some reason after having modified this
code will fail to close the connection which is pretty bad.

Is this the correct version of the code above:
connection = db.open()
try:
    try:
        for each in connection.root()['mycollection']:
            each.setSomeState(arg)
    except:
        connection.getTransaction().abort()
        raise
    connection.getTransaction().commit()
finally:
    connection.close()

The reason I am concerned about this is that I have a relatively large
web based system using ZODB and I don't want the entire web server to
go down just because one of the web pages has a bug in it. Maybe one
should always do connection.getTransaction().abort() before closing
the connection?





More information about the ZODB-Dev mailing list