ZODB: single database, multiple connections

Petra Chong petra.chong at gmail.com
Mon Oct 30 12:32:09 CET 2006


Hello all

I am using Python 2.3 and ZODB (without the rest of Zope) with the
following pattern:

* One process which writes stuff to a ZODB instance (call it test.db)
* Another process which reads stuff from the above ZODB instance
test.db

What I find is that when the first process writes, the second doesn't
pick up the changes. I am sure this must be because I am using ZODB
wrongly, but I can't find any examples that show how to use ZODB in
this way, and I can't find any API docs for FileStorage, Connection,
etc. Reading the source code (from C:\python23\lib\site-packages) has
not thrown up anything useful.

Here's my test code:

A simple database class:

class Database(object):
    PersistentObject = persistent.Persistent
    PersistentDict = BTrees.OOBTree.OOBTree

    def __init__(self, filename, read_only = False):
        self.storage = FileStorage.FileStorage(filename, read_only =
read_only)
        self.db = DB(self.storage)
        self.connection = self.db.open()
        self.dbroot = self.connection.root()


Write:


db = Database("test.db")
db.data = db.get_dictionary('data')

sz = len(db.data.keys())

class Datum(Persistent):
    def __init__(self, value):
        self.value = value

if __name__ == '__main__':
    # insert 10 things
    for i in range(0, 10):
        val = i + sz
        d = Datum(val)
        db.data[val] = d
    transaction.commit()

Read:

db = Database("test.db", read_only = True)

data = db.get_dictionary('data')

If I have a Python shell open and run the above two lines, if I run the
write process repeatedly, the above "data" object never contains any of
the newly added items. To pick them up I have to totally recreate the
"db" object.

I must be doing something wrongly, but I can't figure out what.

Any suggestions?

Thanks,

PC




More information about the Python-list mailing list