Transactions

Introduction

Plone uses the ZODB database which implements Multiversion concurrency control.

Plone will complete either all database modifications that occur during a request, or none of them. It will never write incomplete data to the database.

Plone and the underlying Zope handles transactions transparently.

Note

Every transaction is a read transaction until any of the objects participating in the transaction are mutated (object attribute set), turning the transaction to a write transaction.

Note

Old examples might refer to the get_transaction() function. This has been replaced by transaction.get() in the later Zope versions.

Please read this Zope transaction tutorial to get started how to use transactions with your code.

Using transactions

Normally transactions are managed by Plone and the developer should not be interested in them.

Special cases where one would want to manage transaction life-cycle may include:

  • Batch creation or editing of many items once.

Example code:

Subtransactions

Normally, a Zope transaction keeps a list of objects modified within the transaction in a structure in RAM.

This list of objects can grow quite large when there is a lot of work done across a lot of objects in the context of a transaction. Subtransactions write portions of this object list out to disk, freeing the RAM required by the transaction list. Using subtransactions can allow you to build transactions involving objects whose combined size is larger than available RAM.

Example:

import transaction
...

done = 0
for brain in all_images:
    done += 1
    ...
    # Since this is HUGE operation (think resizing 2 GB images)
    # it is not nice idea to buffer the transaction (all changed data)
    # in the memory (Zope default transaction behavior).
    # Using subtransactions we hint Zope when it would be a good time to
    # flush the changes to the disk.
    if done % 10 == 0:
        # Commit subtransaction for every 10th processed item
        transaction.get().commit(True)

Transaction boundary events

It is possible to perform actions before and after transaction is written to the database.

http://svn.zope.org/transaction/trunk/transaction/_transaction.py?rev=81646&view=auto




Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Go to Transactions on GitHub.
  2. Press Fork and edit this file button.
  3. Edit file contents using GitHub's text editor in your web browserm
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on Github.

For basic information about updating this manual and Sphinx format please see Writing and updating the manual guide.