Django

Code

Ticket #6623: commit_manually_exception_pass_thru.diff

File commit_manually_exception_pass_thru.diff, 2.0 kB (added by justine <jtunney@lobstertech.com>, 4 months ago)
  • django/db/transaction.py

    old new  
    5858    if thread_ident not in dirty: 
    5959        dirty[thread_ident] = False 
    6060 
    61 def leave_transaction_management(): 
     61def leave_transaction_management(exception=None): 
    6262    """ 
    6363    Leaves transaction management for a running thread. A dirty flag is carried 
    6464    over to the surrounding block, as a commit will commit all changes, even 
    6565    those from outside. (Commits are on connection level.) 
     66 
     67    If you are leaving a transaction after you've caught an Exception, pass 
     68    the Exception object to the exception argument and this function will 
     69    re-raise it for you. 
    6670    """ 
    6771    thread_ident = thread.get_ident() 
    6872    if thread_ident in state and state[thread_ident]: 
     
    7175        raise TransactionManagementError("This code isn't under transaction management") 
    7276    if dirty.get(thread_ident, False): 
    7377        rollback() 
    74         raise TransactionManagementError("Transaction managed block ended with pending COMMIT/ROLLBACK") 
     78        if exception: 
     79            raise exception 
     80        else: 
     81            raise TransactionManagementError("Transaction managed block ended with pending COMMIT/ROLLBACK") 
    7582    dirty[thread_ident] = False 
     83    if exception: 
     84        raise exception 
    7685 
    7786def is_dirty(): 
    7887    """ 
     
    219228    themselves. 
    220229    """ 
    221230    def _commit_manually(*args, **kw): 
     231        exp = None 
    222232        try: 
    223             enter_transaction_management() 
    224             managed(True) 
    225             return func(*args, **kw) 
     233            try: 
     234                enter_transaction_management() 
     235                managed(True) 
     236                return func(*args, **kw) 
     237            except Exception, exp: 
     238                pass 
    226239        finally: 
    227             leave_transaction_management(
     240            leave_transaction_management(exp
    228241 
    229242    return wraps(func)(_commit_manually)