Re: Re: how to get current database connection associated with current thread
Jason Madden <[email protected]>
| Newsgroups | gmane.comp.web.zope.zodb |
|---|---|
| Message-ID | <[email protected]> |
> On Aug 24, 2016, at 09:11, Sanjay Rao <[email protected]> wrote: > > I got my answer > obj._p_jar is connection If `obj` is a persistent object that has previously been added to or loaded from a Connection, yes, its _p_jar will be a Connection object. Note, however, that that connection is not necessarily safe to use or associated with the current thread. If you have a bug in your code that allows a persistent object to leak between threads or outlive a transaction boundary when the connection is closed, you can run into trouble. A faulty cache, for example, can produce this situation, leaving you with ConnectionStateErrors: py>> import ZODB, ZODB.DemoStorage, persistent, transaction py>> db = ZODB.DB(ZODB.DemoStorage.DemoStorage()) py>> conn = db.open() py>> plist = persistent.list.PersistentList() py>> conn.root()['key'] = plist # Note that there is no connection yet py>> plist._p_jar # After commit we have a connection py>> transaction.commit() py>> plist._p_jar <Connection at 10728bf30> # But we still have it after the connection is closed py>> conn.close() py>> plist._p_jar <Connection at 10728bf30> # And it is useless py>> plist.thing Traceback (most recent call last): ... ConnectionStateError: Shouldn't load state for persistent.list.PersistentList 0x316490b7e11981b6 when the connection is closed py>> plist._p_jar.root() Traceback (most recent call last): ... ConnectionStateError: The database connection is closed So I guess I'm just saying, be careful about the lifetime of your persistent objects. -- You received this message because you are subscribed to the Google Groups "zodb" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.