app/django/middleware/transaction.py
changeset 54 03e267d67478
equal deleted inserted replaced
53:57b4279d8c4e 54:03e267d67478
       
     1 from django.db import transaction
       
     2 
       
     3 class TransactionMiddleware(object):
       
     4     """
       
     5     Transaction middleware. If this is enabled, each view function will be run
       
     6     with commit_on_response activated - that way a save() doesn't do a direct
       
     7     commit, the commit is done when a successful response is created. If an
       
     8     exception happens, the database is rolled back.
       
     9     """
       
    10     def process_request(self, request):
       
    11         """Enters transaction management"""
       
    12         transaction.enter_transaction_management()
       
    13         transaction.managed(True)
       
    14 
       
    15     def process_exception(self, request, exception):
       
    16         """Rolls back the database and leaves transaction management"""
       
    17         if transaction.is_dirty():
       
    18             transaction.rollback()
       
    19         transaction.leave_transaction_management()
       
    20 
       
    21     def process_response(self, request, response):
       
    22         """Commits and leaves transaction management."""
       
    23         if transaction.is_managed():
       
    24             if transaction.is_dirty():
       
    25                 transaction.commit()
       
    26             transaction.leave_transaction_management()
       
    27         return response