Skip to content

Commit 4ad7e99

Browse files
author
Erlend E. Aasland
committed
Make sure we reset statements both at cursor dealloc and when the
current cursor statement is replaced. This is needed because SELECT queries will lock tables until they are either done or reset.
1 parent 747000c commit 4ad7e99

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

Modules/_sqlite/cursor.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,12 @@ cursor_dealloc(pysqlite_Cursor *self)
117117
PyObject_ClearWeakRefs((PyObject*)self);
118118
}
119119
if (self->statement) {
120-
pysqlite_statement_reset(self->statement);
120+
/* A SELECT query will lock the affected database table(s), so we need
121+
* to reset the statement to unlock the database before disappearing */
122+
sqlite3_stmt *stmt = self->statement->st;
123+
if (sqlite3_stmt_readonly(stmt)) {
124+
pysqlite_statement_reset(self->statement);
125+
}
121126
}
122127
tp->tp_clear((PyObject *)self);
123128
tp->tp_free(self);
@@ -526,6 +531,16 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
526531
Py_SETREF(self->description, Py_None);
527532
self->rowcount = 0L;
528533

534+
if (self->statement) {
535+
/* A SELECT query will lock the affected database table(s), so we need
536+
* to reset the statement to unlock the database before switching
537+
* statements */
538+
sqlite3_stmt *stmt = self->statement->st;
539+
if (sqlite3_stmt_readonly(stmt)) {
540+
pysqlite_statement_reset(self->statement);
541+
}
542+
}
543+
529544
PyObject *stmt = get_statement_from_cache(self, operation);
530545
Py_XSETREF(self->statement, (pysqlite_Statement *)stmt);
531546
if (!self->statement) {

0 commit comments

Comments
 (0)