-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(sqlite): autocommit mode transaction handling to match CPython #5918
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
youknowone
merged 1 commit into
RustPython:main
from
ever0de:fix/sqlite-autocommit-transaction-control
Jul 9, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1346,6 +1346,16 @@ mod _sqlite { | |
if let Some(val) = &val { | ||
begin_statement_ptr_from_isolation_level(val, vm)?; | ||
} | ||
|
||
// If setting isolation_level to None (auto-commit mode), commit any pending transaction | ||
if val.is_none() { | ||
let db = self.db_lock(vm)?; | ||
if !db.is_autocommit() { | ||
// Keep the lock and call implicit_commit directly to avoid race conditions | ||
db.implicit_commit(vm)?; | ||
} | ||
} | ||
|
||
let _ = unsafe { self.isolation_level.swap(val) }; | ||
Ok(()) | ||
} | ||
|
@@ -1472,7 +1482,10 @@ mod _sqlite { | |
|
||
let db = zelf.connection.db_lock(vm)?; | ||
|
||
if stmt.is_dml && db.is_autocommit() { | ||
if stmt.is_dml | ||
&& db.is_autocommit() | ||
&& zelf.connection.isolation_level.deref().is_some() | ||
{ | ||
db.begin_transaction( | ||
zelf.connection | ||
.isolation_level | ||
|
@@ -1552,7 +1565,10 @@ mod _sqlite { | |
|
||
let db = zelf.connection.db_lock(vm)?; | ||
|
||
if stmt.is_dml && db.is_autocommit() { | ||
if stmt.is_dml | ||
&& db.is_autocommit() | ||
&& zelf.connection.isolation_level.deref().is_some() | ||
Comment on lines
+1568
to
+1570
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition let isolation_level = zelf.connection.isolation_level.deref();
if stmt.is_dml
&& db.is_autocommit()
&& isolation_level.is_some() |
||
{ | ||
db.begin_transaction( | ||
zelf.connection | ||
.isolation_level | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition
zelf.connection.isolation_level.deref().is_some()
seems like it could be simplified. Consider extracting thederef()
call to a local variable to improve readability and avoid dereferencing multiple times.