[ZODB-Dev] Transaction

Jeremy Hylton jeremy at zope.com
Tue Jul 15 16:34:41 EDT 2003


On Tue, 2003-07-15 at 05:17, Paolo Invernizzi wrote:
> I'm implementing some python script for managing some 
> files...(creation,move,changes,etc...).
> 
> I'm wondering if is it possible manage this functions transactionally.
> 
> What is the best way to handle that?
> 
> For example...
> 
> class MyFile(Persistent):
>      def work(self):
>         # for example ... copy file1.data __file1.data
>         # work on __file1.data based on self datas...
> 
> 
>      def on_commit(self):
>          # do something on the file when committing
>          # copy __file1.data over file1.data
>          # del __file1.data
> 
>      def on_rollback(self):
>          # the transaction was aborted.
>          del __file1.data
> 
> If I have some MyFile instances, is it possible to have 
> on_commit/on_rollback called when committing or aborting a transaction?

The commit process is a little complicated because it is a two-phase
commit process, but that might work well for you application.  In the
first phase, it should make a backup of the old file and copy the new
data into place.  If something goes wrong here, it can report that the
operation did not succeed.  In the second phase, where the transaction
participant is told to abort or commit (and should not fail to commit),
you could either undo the changes or delete the old/temp copies.

The way to write this is to implement the two-phase commit protocol.
You'd want to implement the tpc_XXX methods and the abort and commit
methods.  I don't know if they're documented anywhere.

Jeremy





More information about the ZODB-Dev mailing list