@@ -34,6 +34,17 @@ def __exit__(self, exception_type, exception_value, exception_traceback):
34
34
pass
35
35
36
36
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
+
37
48
class DatabaseWrapper (BaseDatabaseWrapper ):
38
49
data_types = {
39
50
"AutoField" : "int" ,
@@ -195,18 +206,16 @@ def _driver_info(self):
195
206
return DriverInfo ("django-mongodb-backend" , django_mongodb_backend_version )
196
207
return None
197
208
209
+ @requires_transaction_support
198
210
def _commit (self ):
199
- if not self .features .supports_transactions :
200
- return
201
211
if self .session :
202
212
with debug_transaction (self , "session.commit_transaction()" ):
203
213
self .session .commit_transaction ()
204
214
self .session .end_session ()
205
215
self .session = None
206
216
217
+ @requires_transaction_support
207
218
def _rollback (self ):
208
- if not self .features .supports_transactions :
209
- return
210
219
if self .session :
211
220
with debug_transaction (self , "session.abort_transaction()" ):
212
221
self .session .abort_transaction ()
@@ -218,18 +227,16 @@ def _start_transaction(self):
218
227
with debug_transaction (self , "session.start_transaction()" ):
219
228
self .session .start_transaction ()
220
229
230
+ @requires_transaction_support
221
231
def _start_transaction_under_autocommit (self ):
222
232
# Using this method instead of _set_autocommit()) to start a
223
233
# transaction bypasses BaseDatabaseWrapper.set_autocommit()'s
224
234
# debug_transaction(self, "BEGIN") which is appropriate for a no-SQL
225
235
# backend.
226
- if not self .features .supports_transactions :
227
- return
228
236
self ._start_transaction ()
229
237
238
+ @requires_transaction_support
230
239
def _set_autocommit (self , autocommit , force_begin_transaction_with_broken_autocommit = False ):
231
- if self .features .supports_transactions :
232
- return
233
240
# Besides @transaction.atomic() (which uses
234
241
# _start_transaction_under_autocommit(), disabling autocommit is
235
242
# another way to start a transaction.
@@ -266,9 +273,9 @@ def close_pool(self):
266
273
def cursor (self ):
267
274
return Cursor ()
268
275
276
+ @requires_transaction_support
269
277
def validate_no_broken_transaction (self ):
270
- if self .features .supports_transactions :
271
- super ().validate_no_broken_transaction ()
278
+ super ().validate_no_broken_transaction ()
272
279
273
280
def get_database_version (self ):
274
281
"""Return a tuple of the database's version."""
0 commit comments