View difference between Paste ID: 77H9ns28 and
SHOW: | | - or go back to the newest paste.
1-
1+
import transaction
2
from transaction.interfaces import ISynchronizer
3
from zope.interface import implements
4
5
## A few test transactions... 
6
7
def performSomeTransactions():
8
    ''' A method to perform a few transactions'''
9
    print 'Pre-savepoint'
10
    sp = transaction.savepoint()
11
    print 'Pre-rollback'
12
    sp.rollback()
13
    print 'Pre-commit #1'
14
    transaction.commit()
15
    print 'Pre-begin'
16
    transaction.begin()
17
    print 'Pre-commit #2'
18
    transaction.commit()
19
    print 'Pre-abort'
20
    transaction.abort()
21
    print 'Done.'
22
23
24
## TRANSACTION SYNCHRONIZERS
25
26
class SyncHook(object):
27
    implements( ISynchronizer )
28
    def beforeCompletion(self, transaction):
29
        print '*Before completion*'
30
31
    def afterCompletion(self, transaction):
32
        print '*After completion*'
33
34
    def newTransaction(self, transaction):
35
        print '*New transaction*'
36
37
mySyncHook = SyncHook()
38
# synchronizers are stored as a weak reference
39
transaction.manager.registerSynch( mySyncHook )
40
# perform some transaction so we can see when our synchronizer is called
41
print 'TRANSACTION SYNCHRONIZER'
42
print '------------------------'
43
performSomeTransactions()
44
# unregister our hook
45
transaction.manager.unregisterSynch( mySyncHook )
46
47
48
## BEFORE/AFTER COMMIT HOOKS
49
def myBeforeCommitHook(someVar):
50
    print '*before commit hook*', someVar
51
    
52
def myAfterCommitHook(status, someVar):
53
    print '*after commit hook*', status, someVar
54
55
# register the hooks
56
transaction.get().addBeforeCommitHook( myBeforeCommitHook, args = ('Hello',) )
57
transaction.get().addAfterCommitHook( myAfterCommitHook, kws = { 'someVar' : 'Hello' } )
58
# perform some transaction so we can see when our synchronizer is called
59
print '\n\nBEFORE/AFTER COMMIT HOOKS'
60
print '-------------------------'
61
performSomeTransactions()
62
# no need to unregister the hooks, they are cleared after the transaction
63
#  to which they are attached is over.
64
65
66
## DATA MANAGERS
67
class MyDataManager(object):
68
    transaction_manager = None
69
70
    class Savepoint(object):
71
        def __init__(self, dataManager):
72
            self.dataManager = dataManager
73
            print '*SAVEPOINT*'
74
        
75
        def rollback(self):
76
            print '*ROLLBACK*'
77
78
    def __init__(self):
79
        pass
80
        
81
    def abort(self, transaction):
82
        print '*ABORT*'
83
    
84
    def savepoint(self):
85
        return self.Savepoint(self)
86
87
    def tpc_begin(self, transaction):
88
        print '*TWO PHASE COMMIT BEGIN*'
89
90
    def commit(self, transaction):
91
        print '*COMMIT*'
92
93
    def tpc_vote(self, transaction):
94
        print '*TWO PHASE COMMIT VOTE*'
95
96
    def tpc_finish(self, transaction):
97
        print '*TWO PHASE COMMIT FINISH*'
98
99
    def tpc_abort(self, transaction):
100
        print '*TWO PHASE COMMIT ABORT*'
101
102
    def sortKey(self):
103
        return 0
104
105
# register the custom data manager
106
myDataManager = MyDataManager()
107
transaction.get().join( myDataManager )
108
# perform some transactions so we can see when our data manager is called
109
print '\n\nDATA MANAGER HOOKS'
110
print '-------------------------'
111
performSomeTransactions()
112
# no need to unregister the hooks it is cleared after the transaction
113
#  it joined is finished
114
115
'''
116
The output as of transaction 1.1.0 looks like this:
117
118
TRANSACTION SYNCHRONIZER
119
------------------------
120
Pre-savepoint
121
Pre-rollback
122
Pre-commit #1
123
*Before completion*
124
*After completion*
125
Pre-begin
126
*New transaction*
127
Pre-commit #2
128
*Before completion*
129
*After completion*
130
Pre-abort
131
*Before completion*
132
*After completion*
133
Done.
134
135
136
BEFORE/AFTER COMMIT HOOKS
137
-------------------------
138
Pre-savepoint
139
Pre-rollback
140
Pre-commit #1
141
*before commit hook* Hello
142
*after commit hook* True Hello
143
Pre-begin
144
Pre-commit #2
145
Pre-abort
146
Done.
147
148
149
DATA MANAGER HOOKS
150
-------------------------
151
Pre-savepoint
152
*SAVEPOINT*
153
Pre-rollback
154
*ROLLBACK*
155
Pre-commit #1
156
*TWO PHASE COMMIT BEGIN*
157
*COMMIT*
158
*TWO PHASE COMMIT VOTE*
159
*TWO PHASE COMMIT FINISH*
160
Pre-begin
161
Pre-commit #2
162
Pre-abort
163
Done.
164
'''