@@ -236,6 +236,7 @@ def __init__(
236236 ) -> None :
237237 self ._driver = Driver ()
238238 self ._autocommit = True
239+ self ._in_transaction = False
239240 self .sqlitecloud_connection = sqlitecloud_connection
240241
241242 self .row_factory : Optional [Callable [["Cursor" , Tuple ], object ]] = None
@@ -353,6 +354,9 @@ def commit(self):
353354 """
354355 Commit any pending transactions on database.
355356 """
357+ if not self ._in_transaction :
358+ return
359+
356360 try :
357361 self ._driver .execute ("COMMIT;" , self .sqlitecloud_connection )
358362 except SQLiteCloudOperationalError as e :
@@ -363,6 +367,10 @@ def commit(self):
363367 ):
364368 # compliance to sqlite3
365369 logging .debug (e )
370+ else :
371+ raise
372+
373+ self ._in_transaction = False
366374
367375 def rollback (self ):
368376 """
@@ -373,6 +381,9 @@ def rollback(self):
373381 See the documentation on the `ON CONFLICT <https://docs.sqlitecloud.io/docs/sqlite/lang_conflict>`
374382 clause for additional information about the ROLLBACK conflict resolution algorithm.
375383 """
384+ if not self ._in_transaction :
385+ return
386+
376387 try :
377388 self ._driver .execute ("ROLLBACK;" , self .sqlitecloud_connection )
378389 except SQLiteCloudOperationalError as e :
@@ -383,6 +394,23 @@ def rollback(self):
383394 ):
384395 # compliance to sqlite3
385396 logging .debug (e )
397+ else :
398+ raise
399+
400+ self ._in_transaction = False
401+
402+ def _update_transaction_state (self , sql : str ) -> None :
403+ """Track explicit transaction control statements issued through a cursor."""
404+ parts = sql .lstrip ().split (None , 1 )
405+ if not parts :
406+ return
407+
408+ command = parts [0 ].rstrip (";" ).upper ()
409+
410+ if command == "BEGIN" :
411+ self ._in_transaction = True
412+ elif command in {"COMMIT" , "END" , "ROLLBACK" }:
413+ self ._in_transaction = False
386414
387415 def cursor (self ):
388416 """
@@ -584,6 +612,8 @@ def execute(
584612 sql , parameters , self .connection .sqlitecloud_connection
585613 )
586614
615+ self .connection ._update_transaction_state (sql )
616+
587617 self ._reset ()
588618
589619 if isinstance (result , SQLiteCloudResult ):
0 commit comments