2013-09-23 04:14:17 +08:00
|
|
|
from django.db import (
|
2015-01-28 20:35:27 +08:00
|
|
|
DEFAULT_DB_ALIAS, DatabaseError, Error, ProgrammingError, connections,
|
|
|
|
)
|
2014-08-20 00:32:02 +08:00
|
|
|
from django.utils.decorators import ContextDecorator
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2011-01-17 17:52:47 +08:00
|
|
|
|
2013-09-23 04:14:17 +08:00
|
|
|
class TransactionManagementError(ProgrammingError):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2013-09-23 04:14:17 +08:00
|
|
|
This exception is thrown when transaction management is used improperly.
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2013-02-22 04:26:06 +08:00
|
|
|
def get_connection(using=None):
|
|
|
|
"""
|
|
|
|
Get a database connection by name, or the default database connection
|
2014-03-21 21:21:43 +08:00
|
|
|
if no name is provided. This is a private API.
|
2013-02-22 04:26:06 +08:00
|
|
|
"""
|
|
|
|
if using is None:
|
|
|
|
using = DEFAULT_DB_ALIAS
|
|
|
|
return connections[using]
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2013-03-07 21:09:52 +08:00
|
|
|
def get_autocommit(using=None):
|
|
|
|
"""
|
|
|
|
Get the autocommit status of the connection.
|
|
|
|
"""
|
2013-06-30 21:57:00 +08:00
|
|
|
return get_connection(using).get_autocommit()
|
2013-03-07 21:09:52 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2013-03-11 22:10:58 +08:00
|
|
|
def set_autocommit(autocommit, using=None):
|
2013-03-07 21:09:52 +08:00
|
|
|
"""
|
|
|
|
Set the autocommit status of the connection.
|
|
|
|
"""
|
|
|
|
return get_connection(using).set_autocommit(autocommit)
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def commit(using=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2014-03-21 21:21:43 +08:00
|
|
|
Commits a transaction.
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2013-02-22 04:26:06 +08:00
|
|
|
get_connection(using).commit()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def rollback(using=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2014-03-21 21:21:43 +08:00
|
|
|
Rolls back a transaction.
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2013-02-22 04:26:06 +08:00
|
|
|
get_connection(using).rollback()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def savepoint(using=None):
|
2008-08-12 13:34:56 +08:00
|
|
|
"""
|
|
|
|
Creates a savepoint (if supported and required by the backend) inside the
|
|
|
|
current transaction. Returns an identifier for the savepoint that will be
|
|
|
|
used for the subsequent rollback or commit.
|
|
|
|
"""
|
2013-02-22 04:26:06 +08:00
|
|
|
return get_connection(using).savepoint()
|
2008-08-12 13:34:56 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def savepoint_rollback(sid, using=None):
|
2008-08-12 13:34:56 +08:00
|
|
|
"""
|
|
|
|
Rolls back the most recent savepoint (if one exists). Does nothing if
|
|
|
|
savepoints are not supported.
|
|
|
|
"""
|
2013-02-22 04:26:06 +08:00
|
|
|
get_connection(using).savepoint_rollback(sid)
|
2008-08-12 13:34:56 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def savepoint_commit(sid, using=None):
|
2008-08-12 13:34:56 +08:00
|
|
|
"""
|
|
|
|
Commits the most recent savepoint (if one exists). Does nothing if
|
|
|
|
savepoints are not supported.
|
|
|
|
"""
|
2013-02-22 04:26:06 +08:00
|
|
|
get_connection(using).savepoint_commit(sid)
|
2008-08-12 13:34:56 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2013-03-07 21:09:52 +08:00
|
|
|
def clean_savepoints(using=None):
|
|
|
|
"""
|
|
|
|
Resets the counter used to generate unique savepoint ids in this thread.
|
|
|
|
"""
|
|
|
|
get_connection(using).clean_savepoints()
|
2013-03-05 05:17:35 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2013-06-28 04:19:54 +08:00
|
|
|
def get_rollback(using=None):
|
|
|
|
"""
|
|
|
|
Gets the "needs rollback" flag -- for *advanced use* only.
|
|
|
|
"""
|
2013-06-30 21:57:00 +08:00
|
|
|
return get_connection(using).get_rollback()
|
2013-06-28 04:19:54 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2013-06-28 04:19:54 +08:00
|
|
|
def set_rollback(rollback, using=None):
|
|
|
|
"""
|
|
|
|
Sets or unsets the "needs rollback" flag -- for *advanced use* only.
|
|
|
|
|
|
|
|
When `rollback` is `True`, it triggers a rollback when exiting the
|
|
|
|
innermost enclosing atomic block that has `savepoint=True` (that's the
|
|
|
|
default). Use this to force a rollback without raising an exception.
|
|
|
|
|
|
|
|
When `rollback` is `False`, it prevents such a rollback. Use this only
|
|
|
|
after rolling back to a known-good state! Otherwise, you break the atomic
|
|
|
|
block and data corruption may occur.
|
|
|
|
"""
|
|
|
|
return get_connection(using).set_rollback(rollback)
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2015-07-01 00:18:56 +08:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2013-03-05 05:17:35 +08:00
|
|
|
#################################
|
|
|
|
# Decorators / context managers #
|
|
|
|
#################################
|
|
|
|
|
2014-08-20 00:32:02 +08:00
|
|
|
class Atomic(ContextDecorator):
|
2013-03-05 05:17:35 +08:00
|
|
|
"""
|
|
|
|
This class guarantees the atomic execution of a given block.
|
|
|
|
|
|
|
|
An instance can be used either as a decorator or as a context manager.
|
|
|
|
|
|
|
|
When it's used as a decorator, __call__ wraps the execution of the
|
|
|
|
decorated function in the instance itself, used as a context manager.
|
|
|
|
|
|
|
|
When it's used as a context manager, __enter__ creates a transaction or a
|
|
|
|
savepoint, depending on whether a transaction is already in progress, and
|
|
|
|
__exit__ commits the transaction or releases the savepoint on normal exit,
|
|
|
|
and rolls back the transaction or to the savepoint on exceptions.
|
|
|
|
|
2013-03-08 22:44:41 +08:00
|
|
|
It's possible to disable the creation of savepoints if the goal is to
|
|
|
|
ensure that some code runs within a transaction without creating overhead.
|
|
|
|
|
2013-03-05 05:17:35 +08:00
|
|
|
A stack of savepoints identifiers is maintained as an attribute of the
|
2013-03-08 22:44:41 +08:00
|
|
|
connection. None denotes the absence of a savepoint.
|
2013-03-05 05:17:35 +08:00
|
|
|
|
|
|
|
This allows reentrancy even if the same AtomicWrapper is reused. For
|
2017-04-19 20:47:46 +08:00
|
|
|
example, it's possible to define `oa = atomic('other')` and use `@oa` or
|
2013-03-05 05:17:35 +08:00
|
|
|
`with oa:` multiple times.
|
|
|
|
|
|
|
|
Since database connections are thread-local, this is thread-safe.
|
2014-03-21 21:21:43 +08:00
|
|
|
|
|
|
|
This is a private API.
|
2013-03-05 05:17:35 +08:00
|
|
|
"""
|
|
|
|
|
2013-03-08 22:44:41 +08:00
|
|
|
def __init__(self, using, savepoint):
|
2013-03-05 05:17:35 +08:00
|
|
|
self.using = using
|
2013-03-08 22:44:41 +08:00
|
|
|
self.savepoint = savepoint
|
2013-03-05 05:17:35 +08:00
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
connection = get_connection(self.using)
|
|
|
|
|
2013-03-13 21:08:32 +08:00
|
|
|
if not connection.in_atomic_block:
|
|
|
|
# Reset state when entering an outermost atomic block.
|
|
|
|
connection.commit_on_exit = True
|
|
|
|
connection.needs_rollback = False
|
2013-06-30 21:57:00 +08:00
|
|
|
if not connection.get_autocommit():
|
2013-03-13 21:08:32 +08:00
|
|
|
# Some database adapters (namely sqlite3) don't handle
|
|
|
|
# transactions and savepoints properly when autocommit is off.
|
|
|
|
# Turning autocommit back on isn't an option; it would trigger
|
|
|
|
# a premature commit. Give up if that happens.
|
|
|
|
if connection.features.autocommits_when_autocommit_is_off:
|
|
|
|
raise TransactionManagementError(
|
|
|
|
"Your database backend doesn't behave properly when "
|
|
|
|
"autocommit is off. Turn it on before using 'atomic'.")
|
|
|
|
# Pretend we're already in an atomic block to bypass the code
|
|
|
|
# that disables autocommit to enter a transaction, and make a
|
|
|
|
# note to deal with this case in __exit__.
|
|
|
|
connection.in_atomic_block = True
|
|
|
|
connection.commit_on_exit = False
|
2013-03-05 05:17:35 +08:00
|
|
|
|
|
|
|
if connection.in_atomic_block:
|
2013-03-08 22:44:41 +08:00
|
|
|
# We're already in a transaction; create a savepoint, unless we
|
|
|
|
# were told not to or we're already waiting for a rollback. The
|
|
|
|
# second condition avoids creating useless savepoints and prevents
|
|
|
|
# overwriting needs_rollback until the rollback is performed.
|
|
|
|
if self.savepoint and not connection.needs_rollback:
|
|
|
|
sid = connection.savepoint()
|
|
|
|
connection.savepoint_ids.append(sid)
|
|
|
|
else:
|
|
|
|
connection.savepoint_ids.append(None)
|
2013-03-05 05:17:35 +08:00
|
|
|
else:
|
2015-07-01 00:18:56 +08:00
|
|
|
connection.set_autocommit(False, force_begin_transaction_with_broken_autocommit=True)
|
2013-03-05 05:17:35 +08:00
|
|
|
connection.in_atomic_block = True
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
|
|
|
connection = get_connection(self.using)
|
2013-03-13 21:08:32 +08:00
|
|
|
|
|
|
|
if connection.savepoint_ids:
|
|
|
|
sid = connection.savepoint_ids.pop()
|
|
|
|
else:
|
|
|
|
# Prematurely unset this flag to allow using commit or rollback.
|
|
|
|
connection.in_atomic_block = False
|
|
|
|
|
|
|
|
try:
|
2014-03-24 04:45:31 +08:00
|
|
|
if connection.closed_in_transaction:
|
|
|
|
# The database will perform a rollback by itself.
|
|
|
|
# Wait until we exit the outermost block.
|
|
|
|
pass
|
|
|
|
|
|
|
|
elif exc_type is None and not connection.needs_rollback:
|
2013-03-13 21:08:32 +08:00
|
|
|
if connection.in_atomic_block:
|
|
|
|
# Release savepoint if there is one
|
|
|
|
if sid is not None:
|
|
|
|
try:
|
|
|
|
connection.savepoint_commit(sid)
|
|
|
|
except DatabaseError:
|
2014-04-11 03:14:50 +08:00
|
|
|
try:
|
|
|
|
connection.savepoint_rollback(sid)
|
2014-07-28 20:30:41 +08:00
|
|
|
# The savepoint won't be reused. Release it to
|
|
|
|
# minimize overhead for the database server.
|
|
|
|
connection.savepoint_commit(sid)
|
2014-04-11 03:14:50 +08:00
|
|
|
except Error:
|
|
|
|
# If rolling back to a savepoint fails, mark for
|
|
|
|
# rollback at a higher level and avoid shadowing
|
|
|
|
# the original exception.
|
|
|
|
connection.needs_rollback = True
|
2013-03-13 21:08:32 +08:00
|
|
|
raise
|
|
|
|
else:
|
|
|
|
# Commit transaction
|
2013-03-08 22:44:41 +08:00
|
|
|
try:
|
2013-03-13 21:08:32 +08:00
|
|
|
connection.commit()
|
2013-03-08 22:44:41 +08:00
|
|
|
except DatabaseError:
|
2014-03-24 06:34:14 +08:00
|
|
|
try:
|
|
|
|
connection.rollback()
|
|
|
|
except Error:
|
2014-04-12 05:01:00 +08:00
|
|
|
# An error during rollback means that something
|
|
|
|
# went wrong with the connection. Drop it.
|
2014-03-24 06:34:14 +08:00
|
|
|
connection.close()
|
2013-03-08 22:44:41 +08:00
|
|
|
raise
|
|
|
|
else:
|
2013-03-13 21:08:32 +08:00
|
|
|
# This flag will be set to True again if there isn't a savepoint
|
|
|
|
# allowing to perform the rollback at this level.
|
|
|
|
connection.needs_rollback = False
|
|
|
|
if connection.in_atomic_block:
|
|
|
|
# Roll back to savepoint if there is one, mark for rollback
|
|
|
|
# otherwise.
|
|
|
|
if sid is None:
|
|
|
|
connection.needs_rollback = True
|
2013-03-05 05:17:35 +08:00
|
|
|
else:
|
2014-03-24 03:45:22 +08:00
|
|
|
try:
|
|
|
|
connection.savepoint_rollback(sid)
|
2014-07-28 20:30:41 +08:00
|
|
|
# The savepoint won't be reused. Release it to
|
|
|
|
# minimize overhead for the database server.
|
|
|
|
connection.savepoint_commit(sid)
|
2014-04-11 03:14:50 +08:00
|
|
|
except Error:
|
2014-03-24 03:45:22 +08:00
|
|
|
# If rolling back to a savepoint fails, mark for
|
|
|
|
# rollback at a higher level and avoid shadowing
|
|
|
|
# the original exception.
|
|
|
|
connection.needs_rollback = True
|
2013-03-08 22:44:41 +08:00
|
|
|
else:
|
2013-03-13 21:08:32 +08:00
|
|
|
# Roll back transaction
|
2014-03-24 06:34:14 +08:00
|
|
|
try:
|
|
|
|
connection.rollback()
|
|
|
|
except Error:
|
2014-04-12 05:01:00 +08:00
|
|
|
# An error during rollback means that something
|
|
|
|
# went wrong with the connection. Drop it.
|
2014-03-24 06:34:14 +08:00
|
|
|
connection.close()
|
2013-03-05 05:17:35 +08:00
|
|
|
|
2013-03-13 21:08:32 +08:00
|
|
|
finally:
|
|
|
|
# Outermost block exit when autocommit was enabled.
|
|
|
|
if not connection.in_atomic_block:
|
2014-03-24 04:45:31 +08:00
|
|
|
if connection.closed_in_transaction:
|
|
|
|
connection.connection = None
|
2013-03-13 21:08:32 +08:00
|
|
|
else:
|
|
|
|
connection.set_autocommit(True)
|
|
|
|
# Outermost block exit when autocommit was disabled.
|
|
|
|
elif not connection.savepoint_ids and not connection.commit_on_exit:
|
2014-03-24 04:45:31 +08:00
|
|
|
if connection.closed_in_transaction:
|
|
|
|
connection.connection = None
|
|
|
|
else:
|
|
|
|
connection.in_atomic_block = False
|
2013-03-05 05:17:35 +08:00
|
|
|
|
|
|
|
|
2013-03-08 22:44:41 +08:00
|
|
|
def atomic(using=None, savepoint=True):
|
2013-03-05 05:17:35 +08:00
|
|
|
# Bare decorator: @atomic -- although the first argument is called
|
|
|
|
# `using`, it's actually the function being decorated.
|
|
|
|
if callable(using):
|
2013-03-08 22:44:41 +08:00
|
|
|
return Atomic(DEFAULT_DB_ALIAS, savepoint)(using)
|
2013-03-05 05:17:35 +08:00
|
|
|
# Decorator: @atomic(...) or context manager: with atomic(...): ...
|
|
|
|
else:
|
2013-03-08 22:44:41 +08:00
|
|
|
return Atomic(using, savepoint)
|
2013-03-05 05:17:35 +08:00
|
|
|
|
|
|
|
|
2013-05-19 23:55:12 +08:00
|
|
|
def _non_atomic_requests(view, using):
|
|
|
|
try:
|
|
|
|
view._non_atomic_requests.add(using)
|
|
|
|
except AttributeError:
|
2014-09-26 20:31:50 +08:00
|
|
|
view._non_atomic_requests = {using}
|
2013-05-19 23:55:12 +08:00
|
|
|
return view
|
|
|
|
|
|
|
|
|
|
|
|
def non_atomic_requests(using=None):
|
|
|
|
if callable(using):
|
|
|
|
return _non_atomic_requests(using, DEFAULT_DB_ALIAS)
|
|
|
|
else:
|
|
|
|
if using is None:
|
|
|
|
using = DEFAULT_DB_ALIAS
|
|
|
|
return lambda view: _non_atomic_requests(view, using)
|