Ticket #6623: commit_manually_exception_pass_thru2.diff
File commit_manually_exception_pass_thru2.diff, 2.6 KB (added by , 16 years ago) |
---|
-
django/db/transaction.py
59 59 if thread_ident not in dirty: 60 60 dirty[thread_ident] = False 61 61 62 def leave_transaction_management( ):62 def leave_transaction_management(exception=None): 63 63 """ 64 64 Leaves transaction management for a running thread. A dirty flag is carried 65 65 over to the surrounding block, as a commit will commit all changes, even 66 66 those from outside. (Commits are on connection level.) 67 68 If this function is called from within a finally block where an 69 exception is in the process of working its way up the stack, pass 70 the exception argument so we don't raise a new exception over the 71 pending one. 67 72 """ 68 73 thread_ident = thread.get_ident() 69 74 if thread_ident in state and state[thread_ident]: … … 72 77 raise TransactionManagementError("This code isn't under transaction management") 73 78 if dirty.get(thread_ident, False): 74 79 rollback() 75 raise TransactionManagementError("Transaction managed block ended with pending COMMIT/ROLLBACK") 80 if not exception: 81 raise TransactionManagementError("Transaction managed block ended with pending COMMIT/ROLLBACK") 76 82 dirty[thread_ident] = False 77 83 78 84 def is_dirty(): … … 231 237 control in web apps. 232 238 """ 233 239 def _commit_on_success(*args, **kw): 240 exc = None 234 241 try: 235 242 enter_transaction_management() 236 243 managed(True) 237 244 try: 238 245 res = func(*args, **kw) 239 except :246 except Exception, exc: 240 247 # All exceptions must be handled here (even string ones). 241 248 if is_dirty(): 242 249 rollback() … … 246 253 commit() 247 254 return res 248 255 finally: 249 leave_transaction_management( )256 leave_transaction_management(exc) 250 257 return wraps(func)(_commit_on_success) 251 258 252 259 def commit_manually(func): … … 257 264 themselves. 258 265 """ 259 266 def _commit_manually(*args, **kw): 267 exc = None 260 268 try: 261 enter_transaction_management() 262 managed(True) 263 return func(*args, **kw) 269 try: 270 enter_transaction_management() 271 managed(True) 272 return func(*args, **kw) 273 except Exception, exc: 274 raise 264 275 finally: 265 leave_transaction_management( )276 leave_transaction_management(exc) 266 277 267 278 return wraps(func)(_commit_manually)