Revision 26e15cff snf-common/synnefo/lib/db/xctx.py
b/snf-common/synnefo/lib/db/xctx.py | ||
---|---|---|
57 | 57 |
|
58 | 58 |
|
59 | 59 |
class TransactionContext(object): |
60 |
def __init__(self): |
|
60 |
def __init__(self, **kwargs):
|
|
61 | 61 |
self._rollback = False |
62 | 62 |
|
63 | 63 |
def mark_rollback(self): |
... | ... | |
71 | 71 |
|
72 | 72 |
|
73 | 73 |
class TransactionHandler(object): |
74 |
def __init__(self, ctx=None, using=None, **kwargs): |
|
75 |
self.db = (using if using is not None |
|
76 |
else transaction.DEFAULT_DB_ALIAS) |
|
77 |
self.ctx_class = ctx |
|
78 |
self.ctx_kwargs = kwargs |
|
74 |
def __init__(self, ctx=None, allow_postprocess=True, using=None, **kwargs): |
|
75 |
self.using = using |
|
76 |
self.db = (using if using is not None |
|
77 |
else transaction.DEFAULT_DB_ALIAS) |
|
78 |
self.ctx_class = ctx |
|
79 |
self.ctx_kwargs = kwargs |
|
80 |
self.allow_postprocess = allow_postprocess |
|
79 | 81 |
|
80 | 82 |
def __call__(self, func): |
81 | 83 |
def wrap(*args, **kwargs): |
... | ... | |
105 | 107 |
|
106 | 108 |
def __exit__(self, type, value, traceback): |
107 | 109 |
db = self.db |
110 |
trigger_postprocess = False |
|
108 | 111 |
try: |
109 | 112 |
if value is not None: # exception |
110 | 113 |
if transaction.is_dirty(using=db): |
... | ... | |
120 | 123 |
transaction.rollback(using=db) |
121 | 124 |
raise |
122 | 125 |
else: |
123 |
self.ctx.postprocess() |
|
126 |
trigger_postprocess = True |
|
127 |
else: |
|
128 |
# postprocess, |
|
129 |
# even if there was nothing to commit |
|
130 |
trigger_postprocess = True |
|
124 | 131 |
finally: |
125 | 132 |
transaction.leave_transaction_management(using=db) |
133 |
|
|
134 |
# checking allow_postprocess is needed |
|
135 |
# in order to avoid endless recursion |
|
136 |
if trigger_postprocess and self.allow_postprocess: |
|
137 |
with TransactionHandler(ctx=self.ctx_class, |
|
138 |
allow_postprocess=False, |
|
139 |
using=self.using, |
|
140 |
**self.ctx_kwargs): |
|
141 |
self.ctx.postprocess() |
Also available in: Unified diff