Ticket #6623: commit_manually_better_error_message.diff

File commit_manually_better_error_message.diff, 2.0 KB (added by Thomas Güttler, 16 years ago)
  • django/db/transaction.py

     
    5454    if thread_ident not in dirty:
    5555        dirty[thread_ident] = False
    5656
    57 def leave_transaction_management():
     57def leave_transaction_management(debug_message=''):
    5858    """
    5959    Leaves transaction management for a running thread. A dirty flag is carried
    6060    over to the surrounding block, as a commit will commit all changes, even
    6161    those from outside. (Commits are on connection level.)
     62
     63    If the transaction management gets left, because an exception was raised, it is hard
     64    to find root of the problem. The debug_message gets added to the TransactionManagementError,
     65    if it gets raised. See decorator commit_manually.
    6266    """
    6367    thread_ident = thread.get_ident()
    6468    if thread_ident in state and state[thread_ident]:
     
    6771        raise TransactionManagementError("This code isn't under transaction management")
    6872    if dirty.get(thread_ident, False):
    6973        rollback()
    70         raise TransactionManagementError("Transaction managed block ended with pending COMMIT/ROLLBACK")
     74        raise TransactionManagementError("Transaction managed block ended with pending COMMIT/ROLLBACK %s" % (debug_message))
    7175    dirty[thread_ident] = False
    7276
    7377def is_dirty():
     
    212216    themselves.
    213217    """
    214218    def _commit_manually(*args, **kw):
     219        debug_message=''
    215220        try:
    216221            enter_transaction_management()
    217222            managed(True)
    218223            return func(*args, **kw)
     224        except Exception, exc:
     225            import logging
     226            logging.error('ok')
     227            import sys, traceback
     228            etype, value, tb = sys.exc_info()
     229            debug_message='%r: %s' % (exc, ''.join(traceback.format_exception(etype, value, tb)))
    219230        finally:
    220             leave_transaction_management()
     231            leave_transaction_management(debug_message)
    221232
    222233    return _commit_manually
Back to Top