2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2006-06-01 03:10:43 +08:00
|
|
|
15. Transactions
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
Django handles transactions in three different ways. The default is to commit
|
|
|
|
each transaction upon a write, but you can decorate a function to get
|
|
|
|
commit-on-success behavior. Alternatively, you can manage the transaction
|
|
|
|
manually.
|
|
|
|
"""
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.db import models, DEFAULT_DB_ALIAS
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
class Reporter(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
first_name = models.CharField(max_length=30)
|
|
|
|
last_name = models.CharField(max_length=30)
|
2006-05-02 09:31:56 +08:00
|
|
|
email = models.EmailField()
|
|
|
|
|
2009-06-29 20:29:48 +08:00
|
|
|
class Meta:
|
|
|
|
ordering = ('first_name', 'last_name')
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s %s" % (self.first_name, self.last_name)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2006-08-27 21:59:47 +08:00
|
|
|
__test__ = {'API_TESTS':"""
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> from django.db import connection, transaction
|
2006-08-27 21:59:47 +08:00
|
|
|
"""}
|
2006-06-04 07:45:49 +08:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
building_docs = getattr(settings, 'BUILDING_DOCS', False)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
if building_docs or settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.mysql':
|
2006-08-27 21:59:47 +08:00
|
|
|
__test__['API_TESTS'] += """
|
2006-05-02 09:31:56 +08:00
|
|
|
# the default behavior is to autocommit after each save() action
|
|
|
|
>>> def create_a_reporter_then_fail(first, last):
|
|
|
|
... a = Reporter(first_name=first, last_name=last)
|
|
|
|
... a.save()
|
|
|
|
... raise Exception("I meant to do that")
|
|
|
|
...
|
|
|
|
>>> create_a_reporter_then_fail("Alice", "Smith")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
Exception: I meant to do that
|
|
|
|
|
|
|
|
# The object created before the exception still exists
|
|
|
|
>>> Reporter.objects.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Reporter: Alice Smith>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# the autocommit decorator works exactly the same as the default behavior
|
|
|
|
>>> autocomitted_create_then_fail = transaction.autocommit(create_a_reporter_then_fail)
|
|
|
|
>>> autocomitted_create_then_fail("Ben", "Jones")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
Exception: I meant to do that
|
|
|
|
|
|
|
|
# Same behavior as before
|
|
|
|
>>> Reporter.objects.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Reporter: Alice Smith>, <Reporter: Ben Jones>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2010-03-10 21:13:57 +08:00
|
|
|
# the autocommit decorator also works with a using argument
|
|
|
|
>>> using_autocomitted_create_then_fail = transaction.autocommit(using='default')(create_a_reporter_then_fail)
|
|
|
|
>>> using_autocomitted_create_then_fail("Carol", "Doe")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
Exception: I meant to do that
|
|
|
|
|
|
|
|
# Same behavior as before
|
|
|
|
>>> Reporter.objects.all()
|
|
|
|
[<Reporter: Alice Smith>, <Reporter: Ben Jones>, <Reporter: Carol Doe>]
|
|
|
|
|
|
|
|
# With the commit_on_success decorator, the transaction is only committed if the
|
2006-05-02 09:31:56 +08:00
|
|
|
# function doesn't throw an exception
|
|
|
|
>>> committed_on_success = transaction.commit_on_success(create_a_reporter_then_fail)
|
2010-03-10 21:13:57 +08:00
|
|
|
>>> committed_on_success("Dirk", "Gently")
|
2006-05-02 09:31:56 +08:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
Exception: I meant to do that
|
|
|
|
|
|
|
|
# This time the object never got saved
|
|
|
|
>>> Reporter.objects.all()
|
2010-03-10 21:13:57 +08:00
|
|
|
[<Reporter: Alice Smith>, <Reporter: Ben Jones>, <Reporter: Carol Doe>]
|
|
|
|
|
|
|
|
# commit_on_success decorator also works with a using argument
|
|
|
|
>>> using_committed_on_success = transaction.commit_on_success(using='default')(create_a_reporter_then_fail)
|
|
|
|
>>> using_committed_on_success("Dirk", "Gently")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
Exception: I meant to do that
|
|
|
|
|
|
|
|
# This time the object never got saved
|
|
|
|
>>> Reporter.objects.all()
|
|
|
|
[<Reporter: Alice Smith>, <Reporter: Ben Jones>, <Reporter: Carol Doe>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# If there aren't any exceptions, the data will get saved
|
|
|
|
>>> def remove_a_reporter():
|
|
|
|
... r = Reporter.objects.get(first_name="Alice")
|
|
|
|
... r.delete()
|
|
|
|
...
|
|
|
|
>>> remove_comitted_on_success = transaction.commit_on_success(remove_a_reporter)
|
|
|
|
>>> remove_comitted_on_success()
|
|
|
|
>>> Reporter.objects.all()
|
2010-03-10 21:13:57 +08:00
|
|
|
[<Reporter: Ben Jones>, <Reporter: Carol Doe>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# You can manually manage transactions if you really want to, but you
|
|
|
|
# have to remember to commit/rollback
|
|
|
|
>>> def manually_managed():
|
2010-03-10 21:13:57 +08:00
|
|
|
... r = Reporter(first_name="Dirk", last_name="Gently")
|
2006-05-02 09:31:56 +08:00
|
|
|
... r.save()
|
|
|
|
... transaction.commit()
|
|
|
|
>>> manually_managed = transaction.commit_manually(manually_managed)
|
|
|
|
>>> manually_managed()
|
|
|
|
>>> Reporter.objects.all()
|
2010-03-10 21:13:57 +08:00
|
|
|
[<Reporter: Ben Jones>, <Reporter: Carol Doe>, <Reporter: Dirk Gently>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# If you forget, you'll get bad errors
|
|
|
|
>>> def manually_managed_mistake():
|
2010-03-10 21:13:57 +08:00
|
|
|
... r = Reporter(first_name="Edward", last_name="Woodward")
|
2006-05-02 09:31:56 +08:00
|
|
|
... r.save()
|
|
|
|
... # oops, I forgot to commit/rollback!
|
|
|
|
>>> manually_managed_mistake = transaction.commit_manually(manually_managed_mistake)
|
|
|
|
>>> manually_managed_mistake()
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
TransactionManagementError: Transaction managed block ended with pending COMMIT/ROLLBACK
|
2010-03-10 21:13:57 +08:00
|
|
|
|
|
|
|
# commit_manually also works with a using argument
|
|
|
|
>>> using_manually_managed_mistake = transaction.commit_manually(using='default')(manually_managed_mistake)
|
|
|
|
>>> using_manually_managed_mistake()
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
TransactionManagementError: Transaction managed block ended with pending COMMIT/ROLLBACK
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
"""
|
2010-03-12 22:10:01 +08:00
|
|
|
|
|
|
|
# Regression for #11900: If a function wrapped by commit_on_success writes a
|
|
|
|
# transaction that can't be committed, that transaction should be rolled back.
|
|
|
|
# The bug is only visible using the psycopg2 backend, though
|
|
|
|
# the fix is generally a good idea.
|
|
|
|
pgsql_backends = ('django.db.backends.postgresql_psycopg2', 'postgresql_psycopg2',)
|
|
|
|
if building_docs or settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] in pgsql_backends:
|
|
|
|
__test__['API_TESTS'] += """
|
|
|
|
>>> def execute_bad_sql():
|
|
|
|
... cursor = connection.cursor()
|
|
|
|
... cursor.execute("INSERT INTO transactions_reporter (first_name, last_name) VALUES ('Douglas', 'Adams');")
|
|
|
|
... transaction.set_dirty()
|
|
|
|
...
|
|
|
|
>>> execute_bad_sql = transaction.commit_on_success(execute_bad_sql)
|
|
|
|
>>> execute_bad_sql()
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
IntegrityError: null value in column "email" violates not-null constraint
|
|
|
|
<BLANKLINE>
|
|
|
|
|
2010-03-15 20:58:21 +08:00
|
|
|
>>> transaction.rollback()
|
|
|
|
|
2010-03-12 22:10:01 +08:00
|
|
|
"""
|