[Zope-dev] volatile state maintenance
Itamar Shtull-Trauring
itamars@ibm.net
Sun, 09 Apr 2000 14:35:22 +0300
Sam Gendler wrote:
>
> I have a product which needs to maintain a connection to a 300MB
> database file (Metakt, if you must know). The initial connection takees
> about 3 seconds, due to the size of the file, so I wanted to make a
> product that will keep the connection open, and re-use it.
Cool - Metakit would be really useful for a lot of stuff (sessions,
caching). Any chance for a public release?
As for your question - you can store the connection in a volatile variable
by prefixing it's name with _v_, e.g. _v_mk_connection. You then have to
reconnect only when the object is loaded into memory from the ZODB. You do
this in the __setstate__ method:
def __setstate__(self, state):
""" initialize cache when object gets unpickled from zodb """
# if you don't do this horrible things might happen:
Persistent.__setstate__(self, state)
#create volatile variables:
_v_mkconnection = self.mkconnect()
Check out ZCatalog for a real example.
--
Itamar S.T. itamars@ibm.net