@@ -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+
3748class 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,18 +227,16 @@ 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 # Using this method instead of _set_autocommit()) to start a
223233 # transaction bypasses BaseDatabaseWrapper.set_autocommit()'s
224234 # debug_transaction(self, "BEGIN") which is appropriate for a no-SQL
225235 # backend.
226- if not self .features .supports_transactions :
227- return
228236 self ._start_transaction ()
229237
238+ @requires_transaction_support
230239 def _set_autocommit (self , autocommit , force_begin_transaction_with_broken_autocommit = False ):
231- if self .features .supports_transactions :
232- return
233240 # Besides @transaction.atomic() (which uses
234241 # _start_transaction_under_autocommit(), disabling autocommit is
235242 # another way to start a transaction.
@@ -266,9 +273,9 @@ def close_pool(self):
266273 def cursor (self ):
267274 return Cursor ()
268275
276+ @requires_transaction_support
269277 def validate_no_broken_transaction (self ):
270- if self .features .supports_transactions :
271- super ().validate_no_broken_transaction ()
278+ super ().validate_no_broken_transaction ()
272279
273280 def get_database_version (self ):
274281 """Return a tuple of the database's version."""
0 commit comments