summaryrefslogtreecommitdiff
path: root/django/db/transaction.py
diff options
context:
space:
mode:
authorAndreas Pelme <andreas@pelme.se>2015-06-30 18:18:56 +0200
committerTim Graham <timograham@gmail.com>2015-06-30 14:51:00 -0400
commit00a1d4d042a7afd139316982c9b57e87d26a894f (patch)
tree65b4427112045acc25d097413b34faeab882ad88 /django/db/transaction.py
parent9f0d67137c98aa296471e1b7f57ae43f5bb17db6 (diff)
Fixed #21803 -- Added support for post-commit callbacks
Made it possible to register and run callbacks after a database transaction is committed with the `transaction.on_commit()` function. This patch is heavily based on Carl Meyers django-transaction-hooks <https://django-transaction-hooks.readthedocs.org/>. Thanks to Aymeric Augustin, Carl Meyer, and Tim Graham for review and feedback.
Diffstat (limited to 'django/db/transaction.py')
-rw-r--r--django/db/transaction.py22
1 files changed, 9 insertions, 13 deletions
diff --git a/django/db/transaction.py b/django/db/transaction.py
index d1388675d5..6c174bf2d3 100644
--- a/django/db/transaction.py
+++ b/django/db/transaction.py
@@ -103,6 +103,14 @@ def set_rollback(rollback, using=None):
return get_connection(using).set_rollback(rollback)
+def on_commit(func, using=None):
+ """
+ Register `func` to be called when the current transaction is committed.
+ If the current transaction is rolled back, `func` will not be called.
+ """
+ get_connection(using).on_commit(func)
+
+
#################################
# Decorators / context managers #
#################################
@@ -180,17 +188,7 @@ class Atomic(ContextDecorator):
else:
connection.savepoint_ids.append(None)
else:
- # We aren't in a transaction yet; create one.
- # The usual way to start a transaction is to turn autocommit off.
- # However, some database adapters (namely sqlite3) don't handle
- # transactions and savepoints properly when autocommit is off.
- # In such cases, start an explicit transaction instead, which has
- # the side-effect of disabling autocommit.
- if connection.features.autocommits_when_autocommit_is_off:
- connection._start_transaction_under_autocommit()
- connection.autocommit = False
- else:
- connection.set_autocommit(False)
+ connection.set_autocommit(False, force_begin_transaction_with_broken_autocommit=True)
connection.in_atomic_block = True
def __exit__(self, exc_type, exc_value, traceback):
@@ -272,8 +270,6 @@ class Atomic(ContextDecorator):
if not connection.in_atomic_block:
if connection.closed_in_transaction:
connection.connection = None
- elif connection.features.autocommits_when_autocommit_is_off:
- connection.autocommit = True
else:
connection.set_autocommit(True)
# Outermost block exit when autocommit was disabled.