Skip to content

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Lib/test/test_sqlite3/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ def test_replace_starts_transaction(self):
self.assertEqual(len(res), 1)
self.assertEqual(res[0][0], 5)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_toggle_auto_commit(self):
self.cur1.execute("create table test(i)")
self.cur1.execute("insert into test(i) values (5)")
Expand Down
20 changes: 18 additions & 2 deletions stdlib/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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()
Comment on lines +1485 to +1487

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The condition zelf.connection.isolation_level.deref().is_some() seems like it could be simplified. Consider extracting the deref() call to a local variable to improve readability and avoid dereferencing multiple times.

            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
Expand Down Expand Up @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The condition zelf.connection.isolation_level.deref().is_some() seems like it could be simplified. Consider extracting the deref() call to a local variable to improve readability and avoid dereferencing multiple times.

            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
Expand Down
Loading