[Zope-dev] API for creating sub-ZODBs

Andrew M. Kuchling akuchlin@mems-exchange.org
Tue, 19 Oct 1999 18:18:29 -0400 (EDT)


Recently people were commenting on the recent session tracking HOWTO,
which stores sessions in files inside a directory.  This would be made
much easier if there was some API for creating a single connection to
a new ZODB; your code would have to retrieve the relevant root object,
but you could at least count on it being cleaned up nicely when the
server is shut down.  Another application for this is my ODP code,
which is an amount of data so large that you probably don't want to
mix it in with the rest of your database.  If we have a powerful
object persistency system in the form of the ZODB, why not use it?

In the ODP code release, I have an external method which looks like
this:

from ZODB import FileStorage, DB

# Cache of ZODB objects
ZODBs = {}

def get_odp(filename):
    if ZODBs.has_key( filename ):
        db = ZODBs[filename]
    else:
        # Open ZODB
        fs = FileStorage.FileStorage(filename, read_only = 1)
        db = DB( fs )
        ZODBs[ filename ] = db

    conn = db.open()
    root = conn.root()
    return root['ODP']

This may well be leaking connections; I'm not sure.  Why not add some
function to the Zope API which does this job?  You would provide it
with a Storage class (so you can use a non-versioning storage for
transient sessions) and an argument tuple for the Storage class; if
the Storage had been previously opened, it's returned from a cache.
Invocation would be something like:

conn = Zope.App.openExtraZODB( FileStorage, ('/tmp/mystorage.fs',) )
root = conn.root()
... whatever ...

	Thoughts?  I'm happy to write such a function, if someone will
tell me where it should go.

-- 
A.M. Kuchling			http://starship.python.net/crew/amk/
It was a wasted life, but God forbid that one should be hard upon it, or upon
anything in this world that is not deliberately and coldly wrong . . .
    -- Charles Dickens, in a letter to his friend John Forster.