@@ -135,6 +135,7 @@ def execute(self, sql, *args, **kwargs):
135
135
elif token .value .upper () in ["BEGIN" , "START" ]:
136
136
if self ._in_transaction :
137
137
raise RuntimeError ("transaction already open" )
138
+
138
139
self ._in_transaction = True
139
140
else :
140
141
command = None
@@ -296,8 +297,8 @@ def execute(self, sql, *args, **kwargs):
296
297
assert flask .current_app
297
298
298
299
# Disconnect later - but only once
299
- if not hasattr (self , "teardown_appcontext_added " ):
300
- self .teardown_appcontext_added = True
300
+ if not hasattr (self , "_teardown_appcontext_added " ):
301
+ self ._teardown_appcontext_added = True
301
302
302
303
@flask .current_app .teardown_appcontext
303
304
def shutdown_session (exception = None ):
@@ -320,9 +321,10 @@ def shutdown_session(exception=None):
320
321
_statement = "" .join ([str (bytes ) if token .ttype == sqlparse .tokens .Other else str (token ) for token in tokens ])
321
322
322
323
# If COMMIT or ROLLBACK, turn on autocommit mode
323
- if command in ["COMMIT" , "ROLLBACK" ] and "TO" not in statement :
324
+ if command in ["COMMIT" , "ROLLBACK" ] and "TO" not in ( token . value for token in tokens ) :
324
325
if not self ._in_transaction :
325
326
raise RuntimeError ("transactions must be initiated with BEGIN or START TRANSACTION" )
327
+
326
328
self ._in_transaction = False
327
329
328
330
# Execute statement
@@ -393,6 +395,7 @@ def _close_session(self):
393
395
"""Closes any existing session and resets instance variables."""
394
396
if self ._session is not None :
395
397
self ._session .close ()
398
+
396
399
self ._session = None
397
400
self ._in_transaction = False
398
401
0 commit comments