Skip to content

sqlite3.connect(autocommit=False) breaks executescript("BEGIN; COMMIT") #112441

Closed as not planned
@tom93

Description

@tom93

Bug report

Bug description:

Background: The autocommit parameter was added to the sqlite3 module in Python 3.12 (#83638), the default is currently LEGACY_TRANSACTION_CONTROL but it is set to change to False in a future Python release.

When autocommit is False, it breaks code that uses executescript() with transaction control. Example:

import sqlite3
con = sqlite3.connect(":memory:")
con.executescript("BEGIN TRANSACTION; CREATE TABLE t (x); INSERT INTO t VALUES(1); COMMIT;")
con.close()

This program works on all existing Python versions (2.7 - 3.13.0a1), but with autocommit=False it throws the following error:

Traceback (most recent call last):
  File "<string>", line 4, in <module>
sqlite3.OperationalError: cannot start a transaction within a transaction

So making autocommit=False the default in future will break backward compatibility.


One idea for fixing this is to change the behaviour of executescript() so that when autocommit is False, it will first execute an implicit COMMIT (as it already does when autocommit=LEGACY_TRANSACTION_CONTROL), and after it will execute BEGIN. Edit: This idea is unsuitable, because it is incompatible with the model where a transaction is always open.

Metadata

Metadata

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions