2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
This module implements a transaction manager that can be used to define
|
|
|
|
transaction handling in a request or view function. It is used by transaction
|
|
|
|
control middleware and decorators.
|
|
|
|
|
|
|
|
The transaction manager can be in managed or in auto state. Auto state means the
|
|
|
|
system is using a commit-on-save strategy (actually it's more like
|
|
|
|
commit-on-change). As soon as the .save() or .delete() (or related) methods are
|
|
|
|
called, a commit is made.
|
|
|
|
|
|
|
|
Managed transactions don't do those commits, but will need some kind of manual
|
|
|
|
or implicit commits or rollbacks.
|
|
|
|
"""
|
|
|
|
|
2006-06-01 12:57:10 +08:00
|
|
|
try:
|
|
|
|
import thread
|
|
|
|
except ImportError:
|
|
|
|
import dummy_thread as thread
|
2008-05-29 19:50:50 +08:00
|
|
|
try:
|
|
|
|
from functools import wraps
|
|
|
|
except ImportError:
|
2010-05-04 22:00:30 +08:00
|
|
|
from django.utils.functional import wraps # Python 2.4 fallback.
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.db import connections, DEFAULT_DB_ALIAS
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
class TransactionManagementError(Exception):
|
|
|
|
"""
|
|
|
|
This exception is thrown when something bad happens with transaction
|
|
|
|
management.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
# The states are dictionaries of dictionaries of lists. The key to the outer
|
|
|
|
# dict is the current thread, and the key to the inner dictionary is the
|
|
|
|
# connection alias and the list is handled as a stack of values.
|
2006-05-02 09:31:56 +08:00
|
|
|
state = {}
|
2008-08-12 13:34:56 +08:00
|
|
|
savepoint_state = {}
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# The dirty flag is set by *_unless_managed functions to denote that the
|
|
|
|
# code under transaction management has changed things to require a
|
|
|
|
# database commit.
|
2009-12-22 23:18:51 +08:00
|
|
|
# This is a dictionary mapping thread to a dictionary mapping connection
|
|
|
|
# alias to a boolean.
|
2006-05-02 09:31:56 +08:00
|
|
|
dirty = {}
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def enter_transaction_management(managed=True, using=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
Enters transaction management for a running thread. It must be balanced with
|
|
|
|
the appropriate leave_transaction_management call, since the actual state is
|
|
|
|
managed as a stack.
|
|
|
|
|
|
|
|
The state and dirty flag are carried over from the surrounding block or
|
|
|
|
from the settings, if there is no surrounding block (dirty is always false
|
|
|
|
when no current block is running).
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
if using is None:
|
|
|
|
using = DEFAULT_DB_ALIAS
|
|
|
|
connection = connections[using]
|
2006-05-02 09:31:56 +08:00
|
|
|
thread_ident = thread.get_ident()
|
2009-12-22 23:18:51 +08:00
|
|
|
if thread_ident in state and state[thread_ident].get(using):
|
|
|
|
state[thread_ident][using].append(state[thread_ident][using][-1])
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
2009-12-22 23:18:51 +08:00
|
|
|
state.setdefault(thread_ident, {})
|
|
|
|
state[thread_ident][using] = [settings.TRANSACTIONS_MANAGED]
|
|
|
|
if thread_ident not in dirty or using not in dirty[thread_ident]:
|
|
|
|
dirty.setdefault(thread_ident, {})
|
|
|
|
dirty[thread_ident][using] = False
|
2009-03-11 15:06:50 +08:00
|
|
|
connection._enter_transaction_management(managed)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def leave_transaction_management(using=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
Leaves transaction management for a running thread. A dirty flag is carried
|
|
|
|
over to the surrounding block, as a commit will commit all changes, even
|
|
|
|
those from outside. (Commits are on connection level.)
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
if using is None:
|
|
|
|
using = DEFAULT_DB_ALIAS
|
|
|
|
connection = connections[using]
|
|
|
|
connection._leave_transaction_management(is_managed(using=using))
|
2006-05-02 09:31:56 +08:00
|
|
|
thread_ident = thread.get_ident()
|
2009-12-22 23:18:51 +08:00
|
|
|
if thread_ident in state and state[thread_ident].get(using):
|
|
|
|
del state[thread_ident][using][-1]
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
|
|
|
raise TransactionManagementError("This code isn't under transaction management")
|
2009-12-22 23:18:51 +08:00
|
|
|
if dirty.get(thread_ident, {}).get(using, False):
|
|
|
|
rollback(using=using)
|
2006-05-02 09:31:56 +08:00
|
|
|
raise TransactionManagementError("Transaction managed block ended with pending COMMIT/ROLLBACK")
|
2009-12-22 23:18:51 +08:00
|
|
|
dirty[thread_ident][using] = False
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def is_dirty(using=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
Returns True if the current transaction requires a commit for changes to
|
|
|
|
happen.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
if using is None:
|
|
|
|
using = DEFAULT_DB_ALIAS
|
|
|
|
return dirty.get(thread.get_ident(), {}).get(using, False)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def set_dirty(using=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
Sets a dirty flag for the current thread and code streak. This can be used
|
|
|
|
to decide in a managed block of code to decide whether there are open
|
|
|
|
changes waiting for commit.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
if using is None:
|
|
|
|
using = DEFAULT_DB_ALIAS
|
2006-05-02 09:31:56 +08:00
|
|
|
thread_ident = thread.get_ident()
|
2009-12-22 23:18:51 +08:00
|
|
|
if thread_ident in dirty and using in dirty[thread_ident]:
|
|
|
|
dirty[thread_ident][using] = True
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
|
|
|
raise TransactionManagementError("This code isn't under transaction management")
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def set_clean(using=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
Resets a dirty flag for the current thread and code streak. This can be used
|
|
|
|
to decide in a managed block of code to decide whether a commit or rollback
|
|
|
|
should happen.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
if using is None:
|
|
|
|
using = DEFAULT_DB_ALIAS
|
2006-05-02 09:31:56 +08:00
|
|
|
thread_ident = thread.get_ident()
|
2009-12-22 23:18:51 +08:00
|
|
|
if thread_ident in dirty and using in dirty[thread_ident]:
|
|
|
|
dirty[thread_ident][using] = False
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
|
|
|
raise TransactionManagementError("This code isn't under transaction management")
|
2009-12-22 23:18:51 +08:00
|
|
|
clean_savepoints(using=using)
|
2008-08-12 13:59:43 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def clean_savepoints(using=None):
|
|
|
|
if using is None:
|
|
|
|
using = DEFAULT_DB_ALIAS
|
2008-08-12 13:59:43 +08:00
|
|
|
thread_ident = thread.get_ident()
|
2009-12-22 23:18:51 +08:00
|
|
|
if thread_ident in savepoint_state and using in savepoint_state[thread_ident]:
|
|
|
|
del savepoint_state[thread_ident][using]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def is_managed(using=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
Checks whether the transaction manager is in manual or in auto state.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
if using is None:
|
|
|
|
using = DEFAULT_DB_ALIAS
|
2006-05-02 09:31:56 +08:00
|
|
|
thread_ident = thread.get_ident()
|
2009-12-22 23:18:51 +08:00
|
|
|
if thread_ident in state and using in state[thread_ident]:
|
|
|
|
if state[thread_ident][using]:
|
|
|
|
return state[thread_ident][using][-1]
|
2006-05-02 09:31:56 +08:00
|
|
|
return settings.TRANSACTIONS_MANAGED
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def managed(flag=True, using=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
Puts the transaction manager into a manual state: managed transactions have
|
2006-06-17 03:35:57 +08:00
|
|
|
to be committed explicitly by the user. If you switch off transaction
|
2006-05-02 09:31:56 +08:00
|
|
|
management and there is a pending commit/rollback, the data will be
|
|
|
|
commited.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
if using is None:
|
|
|
|
using = DEFAULT_DB_ALIAS
|
|
|
|
connection = connections[using]
|
2006-05-02 09:31:56 +08:00
|
|
|
thread_ident = thread.get_ident()
|
2009-12-22 23:18:51 +08:00
|
|
|
top = state.get(thread_ident, {}).get(using, None)
|
2006-05-02 09:31:56 +08:00
|
|
|
if top:
|
|
|
|
top[-1] = flag
|
2009-12-22 23:18:51 +08:00
|
|
|
if not flag and is_dirty(using=using):
|
2006-05-02 09:31:56 +08:00
|
|
|
connection._commit()
|
2009-12-22 23:18:51 +08:00
|
|
|
set_clean(using=using)
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
|
|
|
raise TransactionManagementError("This code isn't under transaction management")
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def commit_unless_managed(using=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
Commits changes if the system is not in managed transaction mode.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
if using is None:
|
|
|
|
using = DEFAULT_DB_ALIAS
|
|
|
|
connection = connections[using]
|
|
|
|
if not is_managed(using=using):
|
2006-05-02 09:31:56 +08:00
|
|
|
connection._commit()
|
2009-12-22 23:18:51 +08:00
|
|
|
clean_savepoints(using=using)
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
2009-12-22 23:18:51 +08:00
|
|
|
set_dirty(using=using)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def rollback_unless_managed(using=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
Rolls back changes if the system is not in managed transaction mode.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
if using is None:
|
|
|
|
using = DEFAULT_DB_ALIAS
|
|
|
|
connection = connections[using]
|
|
|
|
if not is_managed(using=using):
|
2006-05-02 09:31:56 +08:00
|
|
|
connection._rollback()
|
|
|
|
else:
|
2009-12-22 23:18:51 +08:00
|
|
|
set_dirty(using=using)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def commit(using=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
Does the commit itself and resets the dirty flag.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
if using is None:
|
|
|
|
using = DEFAULT_DB_ALIAS
|
|
|
|
connection = connections[using]
|
2006-05-02 09:31:56 +08:00
|
|
|
connection._commit()
|
2009-12-22 23:18:51 +08:00
|
|
|
set_clean(using=using)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def rollback(using=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
This function does the rollback itself and resets the dirty flag.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
if using is None:
|
|
|
|
using = DEFAULT_DB_ALIAS
|
|
|
|
connection = connections[using]
|
2006-05-02 09:31:56 +08:00
|
|
|
connection._rollback()
|
2009-12-22 23:18:51 +08:00
|
|
|
set_clean(using=using)
|
2006-05-02 09:31:56 +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.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
if using is None:
|
|
|
|
using = DEFAULT_DB_ALIAS
|
|
|
|
connection = connections[using]
|
2008-08-12 13:34:56 +08:00
|
|
|
thread_ident = thread.get_ident()
|
2009-12-22 23:18:51 +08:00
|
|
|
if thread_ident in savepoint_state and using in savepoint_state[thread_ident]:
|
|
|
|
savepoint_state[thread_ident][using].append(None)
|
2008-08-12 13:34:56 +08:00
|
|
|
else:
|
2009-12-22 23:18:51 +08:00
|
|
|
savepoint_state.setdefault(thread_ident, {})
|
|
|
|
savepoint_state[thread_ident][using] = [None]
|
2008-08-12 13:34:56 +08:00
|
|
|
tid = str(thread_ident).replace('-', '')
|
2009-12-22 23:18:51 +08:00
|
|
|
sid = "s%s_x%d" % (tid, len(savepoint_state[thread_ident][using]))
|
2008-08-12 13:34:56 +08:00
|
|
|
connection._savepoint(sid)
|
|
|
|
return sid
|
|
|
|
|
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.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
if using is None:
|
|
|
|
using = DEFAULT_DB_ALIAS
|
|
|
|
connection = connections[using]
|
|
|
|
thread_ident = thread.get_ident()
|
|
|
|
if thread_ident in savepoint_state and using in savepoint_state[thread_ident]:
|
2008-08-12 13:59:43 +08:00
|
|
|
connection._savepoint_rollback(sid)
|
2008-08-12 13:34:56 +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.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
if using is None:
|
|
|
|
using = DEFAULT_DB_ALIAS
|
|
|
|
connection = connections[using]
|
|
|
|
thread_ident = thread.get_ident()
|
|
|
|
if thread_ident in savepoint_state and using in savepoint_state[thread_ident]:
|
2008-08-12 13:59:43 +08:00
|
|
|
connection._savepoint_commit(sid)
|
2008-08-12 13:34:56 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
##############
|
|
|
|
# DECORATORS #
|
|
|
|
##############
|
|
|
|
|
2010-03-10 21:13:57 +08:00
|
|
|
def autocommit(using=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
Decorator that activates commit on save. This is Django's default behavior;
|
|
|
|
this decorator is useful if you globally activated transaction management in
|
|
|
|
your settings file and want the default behavior in some view functions.
|
|
|
|
"""
|
2010-03-10 21:13:57 +08:00
|
|
|
def inner_autocommit(func, db=None):
|
2009-12-22 23:18:51 +08:00
|
|
|
def _autocommit(*args, **kw):
|
|
|
|
try:
|
2010-03-10 21:13:57 +08:00
|
|
|
enter_transaction_management(managed=False, using=db)
|
|
|
|
managed(False, using=db)
|
2009-12-22 23:18:51 +08:00
|
|
|
return func(*args, **kw)
|
|
|
|
finally:
|
2010-03-10 21:13:57 +08:00
|
|
|
leave_transaction_management(using=db)
|
2009-12-22 23:18:51 +08:00
|
|
|
return wraps(func)(_autocommit)
|
2010-03-10 21:13:57 +08:00
|
|
|
|
|
|
|
# Note that although the first argument is *called* `using`, it
|
|
|
|
# may actually be a function; @autocommit and @autocommit('foo')
|
|
|
|
# are both allowed forms.
|
|
|
|
if using is None:
|
|
|
|
using = DEFAULT_DB_ALIAS
|
|
|
|
if callable(using):
|
|
|
|
return inner_autocommit(using, DEFAULT_DB_ALIAS)
|
|
|
|
return lambda func: inner_autocommit(func, using)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2010-03-10 21:13:57 +08:00
|
|
|
def commit_on_success(using=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
This decorator activates commit on response. This way, if the view function
|
|
|
|
runs successfully, a commit is made; if the viewfunc produces an exception,
|
|
|
|
a rollback is made. This is one of the most common ways to do transaction
|
2010-10-09 16:12:50 +08:00
|
|
|
control in Web apps.
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2010-03-10 21:13:57 +08:00
|
|
|
def inner_commit_on_success(func, db=None):
|
2009-12-22 23:18:51 +08:00
|
|
|
def _commit_on_success(*args, **kw):
|
2006-05-02 09:31:56 +08:00
|
|
|
try:
|
2010-03-10 21:13:57 +08:00
|
|
|
enter_transaction_management(using=db)
|
|
|
|
managed(True, using=db)
|
2009-12-22 23:18:51 +08:00
|
|
|
try:
|
|
|
|
res = func(*args, **kw)
|
|
|
|
except:
|
|
|
|
# All exceptions must be handled here (even string ones).
|
2010-03-10 21:13:57 +08:00
|
|
|
if is_dirty(using=db):
|
|
|
|
rollback(using=db)
|
2009-12-22 23:18:51 +08:00
|
|
|
raise
|
|
|
|
else:
|
2010-03-10 21:13:57 +08:00
|
|
|
if is_dirty(using=db):
|
2010-03-12 22:10:01 +08:00
|
|
|
try:
|
|
|
|
commit(using=db)
|
|
|
|
except:
|
|
|
|
rollback(using=db)
|
|
|
|
raise
|
2009-12-22 23:18:51 +08:00
|
|
|
return res
|
|
|
|
finally:
|
2010-03-10 21:13:57 +08:00
|
|
|
leave_transaction_management(using=db)
|
2009-12-22 23:18:51 +08:00
|
|
|
return wraps(func)(_commit_on_success)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2010-03-10 21:13:57 +08:00
|
|
|
# Note that although the first argument is *called* `using`, it
|
|
|
|
# may actually be a function; @autocommit and @autocommit('foo')
|
|
|
|
# are both allowed forms.
|
|
|
|
if using is None:
|
|
|
|
using = DEFAULT_DB_ALIAS
|
|
|
|
if callable(using):
|
|
|
|
return inner_commit_on_success(using, DEFAULT_DB_ALIAS)
|
|
|
|
return lambda func: inner_commit_on_success(func, using)
|
|
|
|
|
|
|
|
def commit_manually(using=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
Decorator that activates manual transaction control. It just disables
|
|
|
|
automatic transaction control and doesn't do any commit/rollback of its
|
|
|
|
own -- it's up to the user to call the commit and rollback functions
|
|
|
|
themselves.
|
|
|
|
"""
|
2010-03-10 21:13:57 +08:00
|
|
|
def inner_commit_manually(func, db=None):
|
2009-12-22 23:18:51 +08:00
|
|
|
def _commit_manually(*args, **kw):
|
|
|
|
try:
|
2010-03-10 21:13:57 +08:00
|
|
|
enter_transaction_management(using=db)
|
|
|
|
managed(True, using=db)
|
2009-12-22 23:18:51 +08:00
|
|
|
return func(*args, **kw)
|
|
|
|
finally:
|
2010-03-10 21:13:57 +08:00
|
|
|
leave_transaction_management(using=db)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
return wraps(func)(_commit_manually)
|
2010-03-10 21:13:57 +08:00
|
|
|
|
|
|
|
# Note that although the first argument is *called* `using`, it
|
|
|
|
# may actually be a function; @autocommit and @autocommit('foo')
|
|
|
|
# are both allowed forms.
|
|
|
|
if using is None:
|
|
|
|
using = DEFAULT_DB_ALIAS
|
|
|
|
if callable(using):
|
|
|
|
return inner_commit_manually(using, DEFAULT_DB_ALIAS)
|
|
|
|
return lambda func: inner_commit_manually(func, using)
|