One Zope instance with two database

One zope instance two database files

a. Configuring zope.conf

We have to include multiple database files using multiple named <zodb> sections with one <filestorage> key in each which points to the corresponding Data.fs file.

<zodb 1>
    <filestorage>
        path $DATADIR/Data1.fs
    </filestorage>
</zodb>
<zodb 2>
    <filestorage>
        path $DATADIR/Data2.fs
    </filestorage>
</zodb>

We will be referring to the database using the corresponding ZODB label.

b. Connecting to the database programmatically

When Zope starts, it will automatically load the first database entry in zope.conf as default. So now we need to know how we can connect to the second database. The new connection is created using the existing connection.

    connection2 = connection1.get_connection('database_name')

But we need the existing connection to use get_connection(). It is easy if we have an object stored in current database

    siteManager = getSiteManager(some_object)
    defaultFolder = siteManager['default']
    # defaultfolder is an object in first database.
    conn2 = defaultFolder._p_jar.get_connection('2')
    conn2.root()['MyFolder'] = Folder()
    transaction.commit()    # myfolder references to an object in the other database
    myfolder=conn2.root()['MyFolder']
    siteManager.registerUtility(myfolder, IFolder)

c. Storing and retrieving the objects

Z3 referencing scheme is very advance to deal with for cross-database referencing. Once we have stored the object, all we need is a reference to access it. Which means, once we have a reference of the object, we don’t need to connect to database before trying to access the object. It will be totally managed by Z3. Retrieval of the object is the same as we do with any other object,

    # Use getUtility to grab the object faster
    folder_in_db2 = getUtility(IFolder)
    folder_in_db2['some_object'] = SomeObject()

You can assure that the object is stored in the second database by verifying the name of database.

    some_object._p_jar.db().database_name

Reference
zope api documentation on cross database references

0 Responses to “One Zope instance with two database”


  1. No Comments

Leave a Reply