summaryrefslogtreecommitdiff
path: root/django/db/transaction.py
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2008-08-12 05:34:56 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2008-08-12 05:34:56 +0000
commit220993bcc52e78520d8e6cc8aa022608eac10b2a (patch)
treece95383ebb30e67874dbe5b908e693086a4d60c3 /django/db/transaction.py
parente73bf2bdd9a6342e8bf10c78ca94415e02cb8838 (diff)
Added savepoint support to the transaction code.
This is a no-op for most databases. Only necessary on PostgreSQL so that we can do things which will possibly intentionally raise an IntegrityError and not have to rollback the entire transaction. Not supported for PostgreSQL versions prior to 8.0, so should be used sparingly in internal Django code. git-svn-id: http://code.djangoproject.com/svn/django/trunk@8314 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/db/transaction.py')
-rw-r--r--django/db/transaction.py35
1 files changed, 33 insertions, 2 deletions
diff --git a/django/db/transaction.py b/django/db/transaction.py
index cd27cf6044..55fad9e457 100644
--- a/django/db/transaction.py
+++ b/django/db/transaction.py
@@ -19,7 +19,7 @@ except ImportError:
try:
from functools import wraps
except ImportError:
- from django.utils.functional import wraps # Python 2.3, 2.4 fallback.
+ from django.utils.functional import wraps # Python 2.3, 2.4 fallback.
from django.db import connection
from django.conf import settings
@@ -30,9 +30,10 @@ class TransactionManagementError(Exception):
"""
pass
-# The state is a dictionary of lists. The key to the dict is the current
+# The states are dictionaries of lists. The key to the dict is the current
# thread and the list is handled as a stack of values.
state = {}
+savepoint_state = {}
# The dirty flag is set by *_unless_managed functions to denote that the
# code under transaction management has changed things to require a
@@ -164,6 +165,36 @@ def rollback():
connection._rollback()
set_clean()
+def savepoint():
+ """
+ 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.
+ """
+ thread_ident = thread.get_ident()
+ if thread_ident in savepoint_state:
+ savepoint_state[thread_ident].append(None)
+ else:
+ savepoint_state[thread_ident] = [None]
+ tid = str(thread_ident).replace('-', '')
+ sid = "s%s_x%d" % (tid, len(savepoint_state[thread_ident]))
+ connection._savepoint(sid)
+ return sid
+
+def savepoint_rollback(sid):
+ """
+ Rolls back the most recent savepoint (if one exists). Does nothing if
+ savepoints are not supported.
+ """
+ connection._savepoint_rollback(sid)
+
+def savepoint_commit(sid):
+ """
+ Commits the most recent savepoint (if one exists). Does nothing if
+ savepoints are not supported.
+ """
+ connection._savepoint_commit(sid)
+
##############
# DECORATORS #
##############