Skip to content

Commit 5b3b757

Browse files
committed
add @requires_transaction_support
1 parent 6eba5a4 commit 5b3b757

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

django_mongodb_backend/base.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ def __exit__(self, exception_type, exception_value, exception_traceback):
3434
pass
3535

3636

37+
def requires_transaction_support(func):
38+
"""Make a method a no-op if transactions aren't supported."""
39+
40+
def wrapper(self, *args, **kwargs):
41+
if not self.features.supports_transactions:
42+
return
43+
func(self, *args, **kwargs)
44+
45+
return wrapper
46+
47+
3748
class DatabaseWrapper(BaseDatabaseWrapper):
3849
data_types = {
3950
"AutoField": "int",
@@ -195,18 +206,16 @@ def _driver_info(self):
195206
return DriverInfo("django-mongodb-backend", django_mongodb_backend_version)
196207
return None
197208

209+
@requires_transaction_support
198210
def _commit(self):
199-
if not self.features.supports_transactions:
200-
return
201211
if self.session:
202212
with debug_transaction(self, "session.commit_transaction()"):
203213
self.session.commit_transaction()
204214
self.session.end_session()
205215
self.session = None
206216

217+
@requires_transaction_support
207218
def _rollback(self):
208-
if not self.features.supports_transactions:
209-
return
210219
if self.session:
211220
with debug_transaction(self, "session.abort_transaction()"):
212221
self.session.abort_transaction()
@@ -218,19 +227,17 @@ def _start_transaction(self):
218227
with debug_transaction(self, "session.start_transaction()"):
219228
self.session.start_transaction()
220229

230+
@requires_transaction_support
221231
def _start_transaction_under_autocommit(self):
222232
# Implementing this hook (intended only for SQLite), allows
223233
# BaseDatabaseWrapper.set_autocommit() to use it to start a transaction
224234
# rather than set_autocommit(), bypassing set_autocommit()'s call to
225235
# debug_transaction(self, "BEGIN") which isn't semantic for a no-SQL
226236
# backend.
227-
if not self.features.supports_transactions:
228-
return
229237
self._start_transaction()
230238

239+
@requires_transaction_support
231240
def _set_autocommit(self, autocommit, force_begin_transaction_with_broken_autocommit=False):
232-
if self.features.supports_transactions:
233-
return
234241
# Besides @transaction.atomic() (which uses
235242
# _start_transaction_under_autocommit(), disabling autocommit is
236243
# another way to start a transaction.
@@ -267,9 +274,9 @@ def close_pool(self):
267274
def cursor(self):
268275
return Cursor()
269276

277+
@requires_transaction_support
270278
def validate_no_broken_transaction(self):
271-
if self.features.supports_transactions:
272-
super().validate_no_broken_transaction()
279+
super().validate_no_broken_transaction()
273280

274281
def get_database_version(self):
275282
"""Return a tuple of the database's version."""

0 commit comments

Comments
 (0)