[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

Transaction commit and rollback
If your program crashes, your changes are not retained unless you explicitly commit
them to the database.


Automatic updates for some types of in-memory object changes
Objects in ZODB derived from a persistence superclass are smart enough to know
the database must be updated when an attribute is assigned.


Automatic caching of objects
Objects are cached in memory for efficiency and are automatically removed from
the cache when they haven’t been used.


Platform-independent storage
Because ZODB stores your database in a single flat file with large-file support, it is
immune to the potential size constraints and DBM filesystem format differences
of shelves. As we saw earlier in this chapter, a shelve created on Windows using
bsddb may not be accessible to a script running with gdbm on Linux.


Because of such advantages, ZODB is probably worth your attention if you need to
store Python objects in a database persistently in a production environment. The only
significant price you’ll pay for using ZODB is a small amount of extra code:



  • Accessing the database requires a small amount of extra boilerplate code to inter-
    face with ZODB—it’s not a simple open call.

  • Classes are derived from a persistence superclass if you want them to take advant-
    age of automatic updates on changes—persistent classes are generally not as
    completely independent of the database as in shelves, though they can be.


Considering the extra functionality ZODB provides beyond shelves, these trade-offs
are usually more than justified for many applications.


The Mostly Missing ZODB Tutorial


Unfortunately, as I write this edition in June 2010, ZODB is not yet available for Python
3.X, the version used in this book. Because of that, the prior edition’s Python 2.X ex-
amples and material have been removed from this section. However, in deference to
Python 2.X users, as well as 3.X readers of some bright future where a 3.X-base ZODB
has materialized, I’ve made the prior edition’s ZODB materials and examples available
in this edition’s examples package.


See the Preface for details on the examples package, and see these locations within it
for more on ZODB:


C:\...\Dbase\Zodb-2.x # ZODB examples code third edition
C:\...\Dbase\Zodb-2.x\Documentaion # The 3rd Edition's ZODB tutorial

Although I cannot predict the future, ZODB will likely become available for Python
3.X eventually. In the absence of this, other Python-based OODBs may offer additional
3.X options.


1326 | Chapter 17: Databases and Persistence

Free download pdf