[Zope] Standalone ZODB and OOBTree

Tim Peters tim.peters at gmail.com
Tue Feb 8 22:44:26 EST 2005


[Luis N]
> I'm having troubles opening and closing the standalone ZODB with an OOBTree.
>
> def opendb():
>    path = os.path.expanduser('~/zodb/zodb.fs')
>    try:
>        storage = FileStorage.FileStorage(path)
>        db = DB(storage)
>        conn = db.open()
>        dbroot = conn.root()
>        if not dbroot.has_key('authors'):
>            dbroot['authors'] = OOBTree()

Note that since you never commit this transaction, the database is
never changed.

>        dbroot = conn.root()

Note that there's no need for this (you already set dbroot to
conn.root() earlier).

>        return (dbroot, conn, db, storage)
>    except:
>        return False
>
> def closedb(database):
>    try:
>        get_transaction.abort()
>        database[0].close()
>        database[1].close()
>        database[2].close()
>        return True
>    except:
>        return False
> 
> >>> db = opendb()
> >>> db[0]
> {'authors': <BTrees._OOBTree.OOBTree object at 0x82e1dac>}
> >>> db[1]
> <Connection at 082f6d6c>
> >>> db[2]
> <ZODB.DB.DB object at 0x81dd34c>
> >>> closedb(db)
> False
> >>> db[0].close
> Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
> AttributeError: 'PersistentMapping' object has no attribute 'close'

Yes, it doesn't make sense to try to "close" the root object, or to
"close" the OOBTree later.  Those are like trying to close a string,
or a dictionary.  The only thing you need to close (and it is good
practice to close it explicitly) is the database object (db[2] here).

> >>> db[1].close
> <bound method Connection.close of <Connection at 082f6d6c>>

You can close that if you want to, but it's not necessary.

> >>> db[2].close
> <bound method DB.close of <ZODB.DB.DB object at 0x81dd34c>>

That's the important one.  You can also the close the storage (which
you didn't try to do here), but closing the DB also closes the storage
passed originally to the DB constructor.

> How should the OOBTree closed?

Loudly <wink>.  Seriously, closing isn't a concept that applies to
persistent objects.

BTW, note that you'll probably get better answers about ZODB quicker
on the zodb-dev mailing list.


More information about the Zope mailing list