mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Changeset r15232 refactored transactions so that all transaction state is maintained on the connection. This changeset continues that work, moving all transaction control to the connection, too. The transaction control functions in django.db.transaction are left as a generic way to easily apply a transaction control function based on a DB alias. Refs #9964.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@15492 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -13,10 +13,6 @@ or implicit commits or rollbacks.
|
||||
"""
|
||||
import sys
|
||||
|
||||
try:
|
||||
import thread
|
||||
except ImportError:
|
||||
import dummy_thread as thread
|
||||
try:
|
||||
from functools import wraps
|
||||
except ImportError:
|
||||
@@ -46,15 +42,7 @@ def enter_transaction_management(managed=True, using=None):
|
||||
if using is None:
|
||||
using = DEFAULT_DB_ALIAS
|
||||
connection = connections[using]
|
||||
|
||||
if connection.transaction_state:
|
||||
connection.transaction_state.append(connection.transaction_state[-1])
|
||||
else:
|
||||
connection.transaction_state.append(settings.TRANSACTIONS_MANAGED)
|
||||
|
||||
if connection.dirty is None:
|
||||
connection.dirty = False
|
||||
connection._enter_transaction_management(managed)
|
||||
connection.enter_transaction_management(managed)
|
||||
|
||||
def leave_transaction_management(using=None):
|
||||
"""
|
||||
@@ -65,18 +53,7 @@ def leave_transaction_management(using=None):
|
||||
if using is None:
|
||||
using = DEFAULT_DB_ALIAS
|
||||
connection = connections[using]
|
||||
|
||||
connection._leave_transaction_management(is_managed(using=using))
|
||||
if connection.transaction_state:
|
||||
del connection.transaction_state[-1]
|
||||
else:
|
||||
raise TransactionManagementError("This code isn't under transaction "
|
||||
"management")
|
||||
if connection.dirty:
|
||||
rollback(using=using)
|
||||
raise TransactionManagementError("Transaction managed block ended with "
|
||||
"pending COMMIT/ROLLBACK")
|
||||
connection.dirty = False
|
||||
connection.leave_transaction_management()
|
||||
|
||||
def is_dirty(using=None):
|
||||
"""
|
||||
@@ -86,8 +63,7 @@ def is_dirty(using=None):
|
||||
if using is None:
|
||||
using = DEFAULT_DB_ALIAS
|
||||
connection = connections[using]
|
||||
|
||||
return connection.dirty
|
||||
return connection.is_dirty()
|
||||
|
||||
def set_dirty(using=None):
|
||||
"""
|
||||
@@ -98,12 +74,7 @@ def set_dirty(using=None):
|
||||
if using is None:
|
||||
using = DEFAULT_DB_ALIAS
|
||||
connection = connections[using]
|
||||
|
||||
if connection.dirty is not None:
|
||||
connection.dirty = True
|
||||
else:
|
||||
raise TransactionManagementError("This code isn't under transaction "
|
||||
"management")
|
||||
connection.set_dirty()
|
||||
|
||||
def set_clean(using=None):
|
||||
"""
|
||||
@@ -114,18 +85,13 @@ def set_clean(using=None):
|
||||
if using is None:
|
||||
using = DEFAULT_DB_ALIAS
|
||||
connection = connections[using]
|
||||
|
||||
if connection.dirty is not None:
|
||||
connection.dirty = False
|
||||
else:
|
||||
raise TransactionManagementError("This code isn't under transaction management")
|
||||
clean_savepoints(using=using)
|
||||
connection.set_clean()
|
||||
|
||||
def clean_savepoints(using=None):
|
||||
if using is None:
|
||||
using = DEFAULT_DB_ALIAS
|
||||
connection = connections[using]
|
||||
connection.savepoint_state = 0
|
||||
connection.clean_savepoints()
|
||||
|
||||
def is_managed(using=None):
|
||||
"""
|
||||
@@ -134,9 +100,7 @@ def is_managed(using=None):
|
||||
if using is None:
|
||||
using = DEFAULT_DB_ALIAS
|
||||
connection = connections[using]
|
||||
if connection.transaction_state:
|
||||
return connection.transaction_state[-1]
|
||||
return settings.TRANSACTIONS_MANAGED
|
||||
return connection.is_managed()
|
||||
|
||||
def managed(flag=True, using=None):
|
||||
"""
|
||||
@@ -148,16 +112,7 @@ def managed(flag=True, using=None):
|
||||
if using is None:
|
||||
using = DEFAULT_DB_ALIAS
|
||||
connection = connections[using]
|
||||
|
||||
top = connection.transaction_state
|
||||
if top:
|
||||
top[-1] = flag
|
||||
if not flag and is_dirty(using=using):
|
||||
connection._commit()
|
||||
set_clean(using=using)
|
||||
else:
|
||||
raise TransactionManagementError("This code isn't under transaction "
|
||||
"management")
|
||||
connection.managed(flag)
|
||||
|
||||
def commit_unless_managed(using=None):
|
||||
"""
|
||||
@@ -166,11 +121,7 @@ def commit_unless_managed(using=None):
|
||||
if using is None:
|
||||
using = DEFAULT_DB_ALIAS
|
||||
connection = connections[using]
|
||||
if not is_managed(using=using):
|
||||
connection._commit()
|
||||
clean_savepoints(using=using)
|
||||
else:
|
||||
set_dirty(using=using)
|
||||
connection.commit_unless_managed()
|
||||
|
||||
def rollback_unless_managed(using=None):
|
||||
"""
|
||||
@@ -179,10 +130,7 @@ def rollback_unless_managed(using=None):
|
||||
if using is None:
|
||||
using = DEFAULT_DB_ALIAS
|
||||
connection = connections[using]
|
||||
if not is_managed(using=using):
|
||||
connection._rollback()
|
||||
else:
|
||||
set_dirty(using=using)
|
||||
connection.rollback_unless_managed()
|
||||
|
||||
def commit(using=None):
|
||||
"""
|
||||
@@ -191,8 +139,7 @@ def commit(using=None):
|
||||
if using is None:
|
||||
using = DEFAULT_DB_ALIAS
|
||||
connection = connections[using]
|
||||
connection._commit()
|
||||
set_clean(using=using)
|
||||
connection.commit()
|
||||
|
||||
def rollback(using=None):
|
||||
"""
|
||||
@@ -201,8 +148,7 @@ def rollback(using=None):
|
||||
if using is None:
|
||||
using = DEFAULT_DB_ALIAS
|
||||
connection = connections[using]
|
||||
connection._rollback()
|
||||
set_clean(using=using)
|
||||
connection.rollback()
|
||||
|
||||
def savepoint(using=None):
|
||||
"""
|
||||
@@ -213,14 +159,7 @@ def savepoint(using=None):
|
||||
if using is None:
|
||||
using = DEFAULT_DB_ALIAS
|
||||
connection = connections[using]
|
||||
thread_ident = thread.get_ident()
|
||||
|
||||
connection.savepoint_state += 1
|
||||
|
||||
tid = str(thread_ident).replace('-', '')
|
||||
sid = "s%s_x%d" % (tid, connection.savepoint_state)
|
||||
connection._savepoint(sid)
|
||||
return sid
|
||||
return connection.savepoint()
|
||||
|
||||
def savepoint_rollback(sid, using=None):
|
||||
"""
|
||||
@@ -230,9 +169,7 @@ def savepoint_rollback(sid, using=None):
|
||||
if using is None:
|
||||
using = DEFAULT_DB_ALIAS
|
||||
connection = connections[using]
|
||||
|
||||
if connection.savepoint_state:
|
||||
connection._savepoint_rollback(sid)
|
||||
connection.savepoint_rollback(sid)
|
||||
|
||||
def savepoint_commit(sid, using=None):
|
||||
"""
|
||||
@@ -242,9 +179,7 @@ def savepoint_commit(sid, using=None):
|
||||
if using is None:
|
||||
using = DEFAULT_DB_ALIAS
|
||||
connection = connections[using]
|
||||
|
||||
if connection.savepoint_state:
|
||||
connection._savepoint_commit(sid)
|
||||
connection.savepoint_commit(sid)
|
||||
|
||||
##############
|
||||
# DECORATORS #
|
||||
|
||||
Reference in New Issue
Block a user