1
0
mirror of https://github.com/django/django.git synced 2025-07-05 18:29:11 +00:00

[soc2009/multidb] Updated the transaction decorators for use with multiple DB.

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/multidb@10911 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Alex Gaynor 2009-06-03 03:18:48 +00:00
parent 23da5c0ac1
commit b1f70d9e53
5 changed files with 61 additions and 39 deletions

View File

@ -79,10 +79,10 @@ class Comment(BaseCommentAbstractModel):
def __unicode__(self): def __unicode__(self):
return "%s: %s..." % (self.name, self.comment[:50]) return "%s: %s..." % (self.name, self.comment[:50])
def save(self, force_insert=False, force_update=False): def save(self, *args, **kwargs):
if self.submit_date is None: if self.submit_date is None:
self.submit_date = datetime.datetime.now() self.submit_date = datetime.datetime.now()
super(Comment, self).save(force_insert, force_update) super(Comment, self).save(*args, **kwargs)
def _get_userinfo(self): def _get_userinfo(self):
""" """

View File

@ -11,6 +11,7 @@ class Command(AppCommand):
make_option('--database', action='store', dest='database', make_option('--database', action='store', dest='database',
default='default', help='Nominates a database to print the SQL ' default='default', help='Nominates a database to print the SQL '
'for. Defaults to the "default" database.'), 'for. Defaults to the "default" database.'),
) )
output_transaction = True output_transaction = True

View File

@ -11,6 +11,7 @@ class Command(AppCommand):
make_option('--database', action='store', dest='database', make_option('--database', action='store', dest='database',
default='default', help='Nominates a database to print the SQL ' default='default', help='Nominates a database to print the SQL '
'for. Defaults to the "default" database.'), 'for. Defaults to the "default" database.'),
) )
output_transaction = True output_transaction = True

View File

@ -10,6 +10,7 @@ class Command(AppCommand):
make_option('--database', action='store', dest='database', make_option('--database', action='store', dest='database',
default='default', help='Nominates a database to print the SQL ' default='default', help='Nominates a database to print the SQL '
'for. Defaults to the "default" database.'), 'for. Defaults to the "default" database.'),
) )
output_transaction = True output_transaction = True

View File

@ -20,7 +20,7 @@ try:
from functools import wraps from functools import wraps
except ImportError: 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 connections from django.db import connections, DEFAULT_DB_ALIAS
from django.conf import settings from django.conf import settings
class TransactionManagementError(Exception): class TransactionManagementError(Exception):
@ -259,60 +259,79 @@ def savepoint_commit(sid, using=None):
# TODO update all of these for multi-db # TODO update all of these for multi-db
def autocommit(func): def autocommit(using_or_func=None):
""" """
Decorator that activates commit on save. This is Django's default behavior; Decorator that activates commit on save. This is Django's default behavior;
this decorator is useful if you globally activated transaction management in this decorator is useful if you globally activated transaction management in
your settings file and want the default behavior in some view functions. your settings file and want the default behavior in some view functions.
""" """
def _autocommit(*args, **kw): def inner_autocommit(func, using=None):
try: def _autocommit(*args, **kw):
enter_transaction_management(managed=False) try:
managed(False) enter_transaction_management(managed=False, using=using)
return func(*args, **kw) managed(False, using=using)
finally: return func(*args, **kw)
leave_transaction_management() finally:
return wraps(func)(_autocommit) leave_transaction_management(using=using)
return wraps(func)(_autocommit)
if using_or_func is None:
using_or_func = DEFAULT_DB_ALIAS
if callable(using_or_func):
return inner_autocommit(using_or_func, DEFAULT_DB_ALIAS)
return lambda func: inner_autocommit(func, using_or_func)
def commit_on_success(func):
def commit_on_success(func_or_using=None):
""" """
This decorator activates commit on response. This way, if the view function This decorator activates commit on response. This way, if the view function
runs successfully, a commit is made; if the viewfunc produces an exception, 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 a rollback is made. This is one of the most common ways to do transaction
control in web apps. control in web apps.
""" """
def _commit_on_success(*args, **kw): def inner_commit_on_success(func, using=None):
try: def _commit_on_success(*args, **kw):
enter_transaction_management()
managed(True)
try: try:
res = func(*args, **kw) enter_transaction_management(using=using)
except: managed(True, using=using)
# All exceptions must be handled here (even string ones). try:
if is_dirty(): res = func(*args, **kw)
rollback() except:
raise # All exceptions must be handled here (even string ones).
else: if is_dirty(using=using):
if is_dirty(): rollback(using=using)
commit() raise
return res else:
finally: if is_dirty(using=using):
leave_transaction_management() commit(using=using)
return wraps(func)(_commit_on_success) return res
finally:
leave_transaction_management(using=using)
return wraps(func)(_commit_on_success)
if func_or_using is None:
func_or_using = DEFAULT_DB_ALIAS
if callable(func_or_using):
return inner_commit_on_success(func_or_using, DEFAULT_DB_ALIAS)
return lambda func: inner_commit_on_success(func, func_or_using)
def commit_manually(func): def commit_manually(func_or_using=None):
""" """
Decorator that activates manual transaction control. It just disables Decorator that activates manual transaction control. It just disables
automatic transaction control and doesn't do any commit/rollback of its 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 own -- it's up to the user to call the commit and rollback functions
themselves. themselves.
""" """
def _commit_manually(*args, **kw): def inner_commit_manually(func, using=None):
try: def _commit_manually(*args, **kw):
enter_transaction_management() try:
managed(True) enter_transaction_management(using=using)
return func(*args, **kw) managed(True, using=using)
finally: return func(*args, **kw)
leave_transaction_management() finally:
leave_transaction_management(using=using)
return wraps(func)(_commit_manually) return wraps(func)(_commit_manually)
if func_or_using is None:
func_or_using = DEFALUT_DB_ALIAS
if callable(func_or_using):
return inner_commit_manually(func_or_using, DEFAULT_DB_ALIAS)
return lambda func: inner_commit_manually(func, func_or_using)