Developer's Guide

  • Docs Home
  • Community Home

1. Zope Object Database (ZODB)

The ZODB is an object-oriented database used by Zope to store Python objects and their states. For example, modelers maintain information about devices and their configuration in the ZODB.

Zenoss uses ZEO, which is a layer between Zope and the ZODB. ZEO allows for multiple Zope servers to connect to the same ZODB. The ZODB is started and stopped by zeoctl.

Note

ZODBs can be clustered using ZEO, but Zenoss Enterprise customers should contact Customer Support before investigating clustering.

Here is a simple example of using transactions in the ZODB:

...
    import transaction

    ...
    trans= transaction.get()

    # Determine that bad things have happened
    if bad_thing:
        trans.abort()
        # ... any other cleanup required inside the function eg 'return'

    # Life is good!
    # NB: Username or program name -- it's just a text field
    trans.setUser( "zenmodeler" )
    trans.note( "Added good things to xyz object" )
    trans.commit()

The setUser() and note() functions are responsible for creating entries that can be found under the Modifications tab or menu-item.

There are restrictions on what data can be stored, specifically data types that can be pickled. Basic Python data types such as strings, numbers, lists and dictionaries can be pickled, but Python code objects cannot be pickled. In addition, files and sockets cannot be pickled.

Note

The ZODB cannot detect changes to mutable types like lists and dictionaries. In order for changes to be detected, not only is commit() afterwards, but you must explicitly tell the ZODB about the change by modifying a Persistent objects _p_changed attribute.

    # The following imports shouldn't be required in Zenoss code
    # as it should already be taken care of for you.  These are
    # included merely to explicitly show the class dependencies.
    import ZODB
    from Persistence import Persistent
    import transaction
    ...
    class myExampleClass( Persistent ):
        """
        An example class to be used to demonstrate the use of the
        modifying a list and then notifiying ZODB that work needs
        to be done through the _p_changed attribute.
        """
        def __init__(self):
            """
            Initializer
            """
            self.mylist= []

        def addToMyList( self, listItem ):
            """
            Track the listItems that we need
            """
            self.mylist.append( listItem )
            self._p_changed= True  # Notify ZODB

    transaction.commit()

As a general rule, use commit() whenever you want other processes to have access to your database changes. So if a daemon is collecting and Zope needs to do something with the data, run commit() first from the daemon.

This should be enough information to get you started. See ZODB for Python Programmers for more details.