(トランザクション)

はじめに

Plone uses ZODB database <http://en.wikipedia.org/wiki/Zope_Object_database> which follows Multiversion concurrency control pararigm.

During the HTTP request process, Plone completes all modifications of the data or none of them. There cannot end partially written data to database.

Plone and the underlying Zope handles transactions transparently.

ノート

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

ノート

Old examples might refer to 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 transaction 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

目次

前のトピックへ

(オブジェクトの永続化)

次のトピックへ

(オブジェクトのライフサイクル)

このページ