Skip to content

bpo-44042: sqlite3 begin transaction optimisations #25908

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
merged 9 commits into from
Jun 3, 2021
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
36 changes: 0 additions & 36 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,42 +405,6 @@ int pysqlite_check_connection(pysqlite_Connection* con)
}
}

PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
{
int rc;
sqlite3_stmt* statement;

Py_BEGIN_ALLOW_THREADS
rc = sqlite3_prepare_v2(self->db, self->begin_statement, -1, &statement,
NULL);
Py_END_ALLOW_THREADS

if (rc != SQLITE_OK) {
_pysqlite_seterror(self->db);
goto error;
}

rc = pysqlite_step(statement, self);
if (rc != SQLITE_DONE) {
_pysqlite_seterror(self->db);
}

Py_BEGIN_ALLOW_THREADS
rc = sqlite3_finalize(statement);
Py_END_ALLOW_THREADS

if (rc != SQLITE_OK && !PyErr_Occurred()) {
_pysqlite_seterror(self->db);
}

error:
if (PyErr_Occurred()) {
return NULL;
} else {
Py_RETURN_NONE;
}
}

/*[clinic input]
_sqlite3.Connection.commit as pysqlite_connection_commit

Expand Down
2 changes: 0 additions & 2 deletions Modules/_sqlite/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ typedef struct

extern PyTypeObject *pysqlite_ConnectionType;

PyObject* _pysqlite_connection_begin(pysqlite_Connection* self);

int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor);
int pysqlite_check_thread(pysqlite_Connection* self);
int pysqlite_check_connection(pysqlite_Connection* con);
Expand Down
46 changes: 38 additions & 8 deletions Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,38 @@ static int check_cursor(pysqlite_Cursor* cur)
return pysqlite_check_thread(cur->connection) && pysqlite_check_connection(cur->connection);
}

static int
begin_transaction(pysqlite_Connection *self)
{
int rc;
sqlite3_stmt *statement;

Py_BEGIN_ALLOW_THREADS
rc = sqlite3_prepare_v2(self->db, self->begin_statement, -1, &statement,
NULL);
Py_END_ALLOW_THREADS

if (rc != SQLITE_OK) {
_pysqlite_seterror(self->db);
goto error;
}

Py_BEGIN_ALLOW_THREADS
sqlite3_step(statement);
rc = sqlite3_finalize(statement);
Py_END_ALLOW_THREADS

if (rc != SQLITE_OK && !PyErr_Occurred()) {
_pysqlite_seterror(self->db);
}

error:
if (PyErr_Occurred()) {
return -1;
}
return 0;
}

static PyObject *
get_statement_from_cache(pysqlite_Cursor *self, PyObject *operation)
{
Expand All @@ -431,7 +463,6 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
PyObject* parameters = NULL;
int i;
int rc;
PyObject* result;
int numcols;
PyObject* column_name;
sqlite_int64 lastrowid;
Expand Down Expand Up @@ -515,13 +546,12 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation

/* We start a transaction implicitly before a DML statement.
SELECT is the only exception. See #9924. */
if (self->connection->begin_statement && self->statement->is_dml) {
if (sqlite3_get_autocommit(self->connection->db)) {
result = _pysqlite_connection_begin(self->connection);
if (!result) {
goto error;
}
Py_DECREF(result);
if (self->connection->begin_statement
&& self->statement->is_dml
&& sqlite3_get_autocommit(self->connection->db))
{
if (begin_transaction(self->connection) < 0) {
goto error;
}
}

Expand Down