@@ -888,12 +888,15 @@ def insert(
888888 """ # noqa: S608
889889
890890 try :
891- with self .connect () as conn :
892- cursor = conn .cursor ()
893- cursor .execute (insert_sql , values )
894- self ._maybe_commit ()
891+ conn = self .connect ()
892+ cursor = conn .cursor ()
893+ cursor .execute (insert_sql , values )
894+ self ._maybe_commit ()
895895
896896 except sqlite3 .IntegrityError as exc :
897+ # Rollback implicit transaction if not in user-managed transaction
898+ if not self ._in_transaction and self .conn :
899+ self .conn .rollback ()
897900 # Check for foreign key constraint violation
898901 if "FOREIGN KEY constraint failed" in str (exc ):
899902 fk_operation = "insert"
@@ -903,6 +906,9 @@ def insert(
903906 ) from exc
904907 raise RecordInsertionError (table_name ) from exc
905908 except sqlite3 .Error as exc :
909+ # Rollback implicit transaction if not in user-managed transaction
910+ if not self ._in_transaction and self .conn :
911+ self .conn .rollback ()
906912 raise RecordInsertionError (table_name ) from exc
907913 else :
908914 self ._cache_invalidate_table (table_name )
@@ -936,10 +942,10 @@ def get(
936942 """ # noqa: S608
937943
938944 try :
939- with self .connect () as conn :
940- cursor = conn .cursor ()
941- cursor .execute (select_sql , (primary_key_value ,))
942- result = cursor .fetchone ()
945+ conn = self .connect ()
946+ cursor = conn .cursor ()
947+ cursor .execute (select_sql , (primary_key_value ,))
948+ result = cursor .fetchone ()
943949
944950 if result :
945951 result_dict = {
@@ -990,18 +996,26 @@ def update(self, model_instance: BaseDBModel) -> None:
990996 """ # noqa: S608
991997
992998 try :
993- with self .connect () as conn :
994- cursor = conn .cursor ()
995- cursor .execute (update_sql , (* values , primary_key_value ))
999+ conn = self .connect ()
1000+ cursor = conn .cursor ()
1001+ cursor .execute (update_sql , (* values , primary_key_value ))
9961002
997- # Check if any rows were updated
998- if cursor .rowcount == 0 :
999- raise RecordNotFoundError (primary_key_value )
1003+ # Check if any rows were updated
1004+ if cursor .rowcount == 0 :
1005+ raise RecordNotFoundError (primary_key_value ) # noqa: TRY301
10001006
1001- self ._maybe_commit ()
1002- self ._cache_invalidate_table (table_name )
1007+ self ._maybe_commit ()
1008+ self ._cache_invalidate_table (table_name )
10031009
1010+ except RecordNotFoundError :
1011+ # Rollback implicit transaction if not in user-managed transaction
1012+ if not self ._in_transaction and self .conn :
1013+ self .conn .rollback ()
1014+ raise
10041015 except sqlite3 .Error as exc :
1016+ # Rollback implicit transaction if not in user-managed transaction
1017+ if not self ._in_transaction and self .conn :
1018+ self .conn .rollback ()
10051019 raise RecordUpdateError (table_name ) from exc
10061020
10071021 def delete (
@@ -1026,15 +1040,23 @@ def delete(
10261040 """ # noqa: S608
10271041
10281042 try :
1029- with self .connect () as conn :
1030- cursor = conn .cursor ()
1031- cursor .execute (delete_sql , (primary_key_value ,))
1043+ conn = self .connect ()
1044+ cursor = conn .cursor ()
1045+ cursor .execute (delete_sql , (primary_key_value ,))
10321046
1033- if cursor .rowcount == 0 :
1034- raise RecordNotFoundError (primary_key_value )
1035- self ._maybe_commit ()
1036- self ._cache_invalidate_table (table_name )
1047+ if cursor .rowcount == 0 :
1048+ raise RecordNotFoundError (primary_key_value ) # noqa: TRY301
1049+ self ._maybe_commit ()
1050+ self ._cache_invalidate_table (table_name )
1051+ except RecordNotFoundError :
1052+ # Rollback implicit transaction if not in user-managed transaction
1053+ if not self ._in_transaction and self .conn :
1054+ self .conn .rollback ()
1055+ raise
10371056 except sqlite3 .IntegrityError as exc :
1057+ # Rollback implicit transaction if not in user-managed transaction
1058+ if not self ._in_transaction and self .conn :
1059+ self .conn .rollback ()
10381060 # Check for foreign key constraint violation (RESTRICT)
10391061 if "FOREIGN KEY constraint failed" in str (exc ):
10401062 fk_operation = "delete"
@@ -1044,6 +1066,9 @@ def delete(
10441066 ) from exc
10451067 raise RecordDeletionError (table_name ) from exc
10461068 except sqlite3 .Error as exc :
1069+ # Rollback implicit transaction if not in user-managed transaction
1070+ if not self ._in_transaction and self .conn :
1071+ self .conn .rollback ()
10471072 raise RecordDeletionError (table_name ) from exc
10481073
10491074 def select (
0 commit comments