@@ -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,19 +227,17 @@ 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
# Implementing this hook (intended only for SQLite), allows
223
233
# BaseDatabaseWrapper.set_autocommit() to use it to start a transaction
224
234
# rather than set_autocommit(), bypassing set_autocommit()'s call to
225
235
# debug_transaction(self, "BEGIN") which isn't semantic for a no-SQL
226
236
# backend.
227
- if not self .features .supports_transactions :
228
- return
229
237
self ._start_transaction ()
230
238
239
+ @requires_transaction_support
231
240
def _set_autocommit (self , autocommit , force_begin_transaction_with_broken_autocommit = False ):
232
- if self .features .supports_transactions :
233
- return
234
241
# Besides @transaction.atomic() (which uses
235
242
# _start_transaction_under_autocommit(), disabling autocommit is
236
243
# another way to start a transaction.
@@ -267,9 +274,9 @@ def close_pool(self):
267
274
def cursor (self ):
268
275
return Cursor ()
269
276
277
+ @requires_transaction_support
270
278
def validate_no_broken_transaction (self ):
271
- if self .features .supports_transactions :
272
- super ().validate_no_broken_transaction ()
279
+ super ().validate_no_broken_transaction ()
273
280
274
281
def get_database_version (self ):
275
282
"""Return a tuple of the database's version."""
0 commit comments