Skip to content

Commit 23dd575

Browse files
committed
no commit_on_exit
1 parent ac11277 commit 23dd575

File tree

4 files changed

+21
-78
lines changed

4 files changed

+21
-78
lines changed

django_mongodb_backend/base.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,6 @@ def __exit__(self, exception_type, exception_value, exception_traceback):
3636
pass
3737

3838

39-
def requires_transaction_support(func):
40-
"""Make a method a no-op if transactions aren't supported."""
41-
42-
def wrapper(self, *args, **kwargs):
43-
if not self.features._supports_transactions:
44-
return
45-
func(self, *args, **kwargs)
46-
47-
return wrapper
48-
49-
5039
logger = logging.getLogger("django.db.backends.base")
5140

5241

@@ -171,9 +160,6 @@ def __init__(self, settings_dict, alias=DEFAULT_DB_ALIAS):
171160
self.nested_atomics = 0
172161
# Stack of active 'atomic' blocks.
173162
self.atomic_blocks_mongo = []
174-
# Tracks if the outermost 'atomic' block should commit on exit,
175-
# ie. if autocommit was active on entry.
176-
self.commit_on_exit_mongo = True
177163
# Tracks if the transaction should be rolled back to the next
178164
# available savepoint because of an exception in an inner block.
179165
self.needs_rollback_mongo = False
@@ -268,7 +254,6 @@ def close_pool(self):
268254
def cursor(self):
269255
return Cursor()
270256

271-
@requires_transaction_support
272257
def validate_no_broken_transaction(self):
273258
if self.needs_rollback_mongo:
274259
raise TransactionManagementError(

django_mongodb_backend/transaction.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,7 @@ def __enter__(self):
3939
connection = get_connection(self.using)
4040
if not connection.in_atomic_block_mongo:
4141
# Reset state when entering an outermost atomic block.
42-
connection.commit_on_exit_mongo = True
4342
connection.needs_rollback_mongo = False
44-
# if not connection.get_autocommit():
45-
# Pretend we're already in an atomic block to bypass the code
46-
# that disables autocommit to enter a transaction, and make a
47-
# note to deal with this case in __exit__.
48-
# connection.in_atomic_block_mongo = True
49-
# connection.commit_on_exit = False
5043

5144
if connection.in_atomic_block_mongo:
5245
# We're already in a transaction. Increment the number of nested atomics.
@@ -102,14 +95,12 @@ def __exit__(self, exc_type, exc_value, traceback):
10295
# went wrong with the connection. Drop it.
10396
connection.close()
10497
finally:
105-
# Outermost block exit when autocommit was enabled.
106-
if not connection.in_atomic_block_mongo:
107-
if connection.run_commit_hooks_on_set_autocommit_on:
108-
connection.run_and_clear_commit_hooks()
109-
# connection.set_autocommit(True)
110-
# Outermost block exit when autocommit was disabled.
111-
elif not connection.commit_on_exit:
112-
connection.in_atomic_block_mongo = False
98+
# Outermost block exit
99+
if (
100+
not connection.in_atomic_block_mongo
101+
and connection.run_commit_hooks_on_set_autocommit_on
102+
):
103+
connection.run_and_clear_commit_hooks()
113104

114105

115106
def atomic(using=None):

tests/transaction_hooks_/tests.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@ class ForcedError(Exception):
1212

1313
@skipUnlessDBFeature("_supports_transactions")
1414
class TestConnectionOnCommit(TransactionTestCase):
15-
"""
16-
Tests for transaction.on_commit().
17-
18-
Creation/checking of database objects in parallel with callback tracking is
19-
to verify that the behavior of the two match in all tested cases.
20-
"""
15+
"""Largely copied from Django's test/transaction_hooks."""
2116

2217
available_apps = ["transaction_hooks_"]
2318

tests/transactions_/tests.py

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from unittest import expectedFailure
2-
3-
from django.test import TransactionTestCase, skipUnlessDBFeature
1+
from django.db import DatabaseError
2+
from django.test import TransactionTestCase, skipIfDBFeature, skipUnlessDBFeature
43

54
from django_mongodb_backend import transaction
65

@@ -9,16 +8,7 @@
98

109
@skipUnlessDBFeature("_supports_transactions")
1110
class AtomicTests(TransactionTestCase):
12-
"""
13-
Tests for the atomic decorator and context manager.
14-
15-
The tests make assertions on internal attributes because there isn't a
16-
robust way to ask the database for its current transaction state.
17-
18-
Since the decorator syntax is converted into a context manager (see the
19-
implementation), there are only a few basic tests with the decorator
20-
syntax and the bulk of the tests use the context manager syntax.
21-
"""
11+
"""Largely copied from Django's test/transactions."""
2212

2313
available_apps = ["transactions_"]
2414

@@ -76,15 +66,6 @@ def test_nested_commit_commit(self):
7666
reporter2 = Reporter.objects.create(first_name="Archibald", last_name="Haddock")
7767
self.assertSequenceEqual(Reporter.objects.all(), [reporter2, reporter1])
7868

79-
@expectedFailure
80-
def test_nested_commit_rollback(self):
81-
with transaction.atomic():
82-
reporter = Reporter.objects.create(first_name="Tintin")
83-
with self.assertRaisesMessage(Exception, "Oops"), transaction.atomic():
84-
Reporter.objects.create(first_name="Haddock")
85-
raise Exception("Oops, that's his last name")
86-
self.assertSequenceEqual(Reporter.objects.all(), [reporter])
87-
8869
def test_nested_rollback_commit(self):
8970
with self.assertRaisesMessage(Exception, "Oops"), transaction.atomic():
9071
Reporter.objects.create(last_name="Tintin")
@@ -111,16 +92,6 @@ def test_reuse_commit_commit(self):
11192
reporter2 = Reporter.objects.create(first_name="Archibald", last_name="Haddock")
11293
self.assertSequenceEqual(Reporter.objects.all(), [reporter2, reporter1])
11394

114-
@expectedFailure
115-
def test_reuse_commit_rollback(self):
116-
atomic = transaction.atomic()
117-
with atomic:
118-
reporter = Reporter.objects.create(first_name="Tintin")
119-
with self.assertRaisesMessage(Exception, "Oops"), atomic:
120-
Reporter.objects.create(first_name="Haddock")
121-
raise Exception("Oops, that's his last name")
122-
self.assertSequenceEqual(Reporter.objects.all(), [reporter])
123-
12495
def test_reuse_rollback_commit(self):
12596
atomic = transaction.atomic()
12697
with self.assertRaisesMessage(Exception, "Oops"), atomic:
@@ -141,16 +112,6 @@ def test_reuse_rollback_rollback(self):
141112
raise Exception("Oops, that's his first name")
142113
self.assertSequenceEqual(Reporter.objects.all(), [])
143114

144-
# class AtomicInsideTransactionTests(AtomicTests):
145-
# """All basic tests for atomic should also pass within an existing transaction."""
146-
147-
# def setUp(self):
148-
# self.atomic = transaction.atomic()
149-
# self.atomic.__enter__()
150-
151-
# def tearDown(self):
152-
# self.atomic.__exit__(*sys.exc_info())
153-
154115
def test_wrap_callable_instance(self):
155116
"""Atomic can wrap callable instances."""
156117

@@ -160,3 +121,14 @@ def __call__(self):
160121

161122
# Must not raise an exception
162123
transaction.atomic(Callable())
124+
125+
126+
@skipIfDBFeature("_supports_transactions")
127+
class AtomicNotSupportedTests(TransactionTestCase):
128+
available_apps = ["transactions_"]
129+
130+
def test_not_supported(self):
131+
# MongoDB error:
132+
# "Transaction numbers are only allowed on a replica set member or mongos"
133+
with self.assertRaises(DatabaseError), transaction.atomic():
134+
Reporter.objects.create(first_name="Haddock")

0 commit comments

Comments
 (0)