Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import transaction
- from transaction.interfaces import ISynchronizer
- from zope.interface import implements
- ## A few test transactions...
- def performSomeTransactions():
- ''' A method to perform a few transactions'''
- print 'Pre-savepoint'
- sp = transaction.savepoint()
- print 'Pre-rollback'
- sp.rollback()
- print 'Pre-commit #1'
- transaction.commit()
- print 'Pre-begin'
- transaction.begin()
- print 'Pre-commit #2'
- transaction.commit()
- print 'Pre-abort'
- transaction.abort()
- print 'Done.'
- ## TRANSACTION SYNCHRONIZERS
- class SyncHook(object):
- implements( ISynchronizer )
- def beforeCompletion(self, transaction):
- print '*Before completion*'
- def afterCompletion(self, transaction):
- print '*After completion*'
- def newTransaction(self, transaction):
- print '*New transaction*'
- mySyncHook = SyncHook()
- # synchronizers are stored as a weak reference
- transaction.manager.registerSynch( mySyncHook )
- # perform some transaction so we can see when our synchronizer is called
- print 'TRANSACTION SYNCHRONIZER'
- print '------------------------'
- performSomeTransactions()
- # unregister our hook
- transaction.manager.unregisterSynch( mySyncHook )
- ## BEFORE/AFTER COMMIT HOOKS
- def myBeforeCommitHook(someVar):
- print '*before commit hook*', someVar
- def myAfterCommitHook(status, someVar):
- print '*after commit hook*', status, someVar
- # register the hooks
- transaction.get().addBeforeCommitHook( myBeforeCommitHook, args = ('Hello',) )
- transaction.get().addAfterCommitHook( myAfterCommitHook, kws = { 'someVar' : 'Hello' } )
- # perform some transaction so we can see when our synchronizer is called
- print '\n\nBEFORE/AFTER COMMIT HOOKS'
- print '-------------------------'
- performSomeTransactions()
- # no need to unregister the hooks, they are cleared after the transaction
- # to which they are attached is over.
- ## DATA MANAGERS
- class MyDataManager(object):
- transaction_manager = None
- class Savepoint(object):
- def __init__(self, dataManager):
- self.dataManager = dataManager
- print '*SAVEPOINT*'
- def rollback(self):
- print '*ROLLBACK*'
- def __init__(self):
- pass
- def abort(self, transaction):
- print '*ABORT*'
- def savepoint(self):
- return self.Savepoint(self)
- def tpc_begin(self, transaction):
- print '*TWO PHASE COMMIT BEGIN*'
- def commit(self, transaction):
- print '*COMMIT*'
- def tpc_vote(self, transaction):
- print '*TWO PHASE COMMIT VOTE*'
- def tpc_finish(self, transaction):
- print '*TWO PHASE COMMIT FINISH*'
- def tpc_abort(self, transaction):
- print '*TWO PHASE COMMIT ABORT*'
- def sortKey(self):
- return 0
- # register the custom data manager
- myDataManager = MyDataManager()
- transaction.get().join( myDataManager )
- # perform some transactions so we can see when our data manager is called
- print '\n\nDATA MANAGER HOOKS'
- print '-------------------------'
- performSomeTransactions()
- # no need to unregister the hooks it is cleared after the transaction
- # it joined is finished
- '''
- The output as of transaction 1.1.0 looks like this:
- TRANSACTION SYNCHRONIZER
- ------------------------
- Pre-savepoint
- Pre-rollback
- Pre-commit #1
- *Before completion*
- *After completion*
- Pre-begin
- *New transaction*
- Pre-commit #2
- *Before completion*
- *After completion*
- Pre-abort
- *Before completion*
- *After completion*
- Done.
- BEFORE/AFTER COMMIT HOOKS
- -------------------------
- Pre-savepoint
- Pre-rollback
- Pre-commit #1
- *before commit hook* Hello
- *after commit hook* True Hello
- Pre-begin
- Pre-commit #2
- Pre-abort
- Done.
- DATA MANAGER HOOKS
- -------------------------
- Pre-savepoint
- *SAVEPOINT*
- Pre-rollback
- *ROLLBACK*
- Pre-commit #1
- *TWO PHASE COMMIT BEGIN*
- *COMMIT*
- *TWO PHASE COMMIT VOTE*
- *TWO PHASE COMMIT FINISH*
- Pre-begin
- Pre-commit #2
- Pre-abort
- Done.
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement