ZODB.Transaction Management

Documentation
This package documents ttransaction-management features.

There is no more than one transaction object per thread. Each transaction object may be used for multiple logical transactions. At transaction boundaries, the transaction object is reused when the old transaction is completed and a new transaction begins. When Z ODB 3 is imported, it defines a built-in function:

get_transaction()

An application calls this function to obtain the transaction for the current thread and calls methods on the transaction to define transaction boundaries and to set transaction meta-data.

The API diagram summarizes the transaction management interface used by applications.

The Normal Commit diagram illustrates the sequence of actions in a normal transactions with a scenario. The Abort Due to Conflict diagram shows what happens when a transaction must be aborted due to conflicting changes.
Two-phase commit
The Z ODB 3 transaction manager employs a two-phase commit protocol.
Subtransactions
Subtransactions provide a means to commit intermediate work making up a transaction. There are two reasons to do this:

- Less memory is used. Objects that have uncommitted changes can't be removed from memory by the ZODB cache manager. Commiting intermediate work allows objects to be removed from memory without sacrificing overall trasactional semantics.

- It makes it possible to recover from partial faulures. For example, suppose, in a transaction, a large amount of work is performed. This work can be checkpointed by commiting a subtransaction.
Additional work can be performed and, if an error occurs, aborted. Objects are restored to their states as of the last commit.

Subtransactions are created by providing true arguments to transaction commit and abort methods.

Here's an example:

foo.x=1 # and do alot of other comptation
get_transaction().commit(1)
foo.x=2 # and do alot of other comptation
get_transaction().abort(1)
# The work done since the last
# commit is undone. foo.x is 1.
foo.y=1 # and do alot of other comptation
get_transaction().commit()
# All work done before the first commit and
# work done between the abort and the
# last commit is made permanent.
# foo.x and foo.y are 1.