Skip to content
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

bugfix: after executing statement table lock was not cleared #1195

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -2083,6 +2083,12 @@ func (s *SQLiteStmt) execSync(args []driver.NamedValue) (driver.Result, error) {
return nil, err
}

rv = C.sqlite3_reset(s.s)
if rv != C.SQLITE_OK {
Copy link
Author

Choose a reason for hiding this comment

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

Per the sqlite documentation (ref),

If the most recent call to sqlite3_step(S) for the prepared statement S indicated an error, then sqlite3_reset(S) returns an appropriate error code. The sqlite3_reset(S) interface might also return an error code if there were no prior errors but the process of resetting the prepared statement caused a new error. For example, if an INSERT statement with a RETURNING clause is only stepped one time, that one call to sqlite3_step(S) might return SQLITE_ROW but the overall statement might still fail and the sqlite3_reset(S) call might return SQLITE_BUSY if locking constraints prevent the database change from committing. Therefore, it is important that applications check the return code from sqlite3_reset(S) even if no prior call to sqlite3_step(S) indicated a problem.

that is why we consider the execution failed if reset returns an error code.

err := s.c.lastError()
C.sqlite3_clear_bindings(s.s)
return nil, err
}
return &SQLiteResult{id: int64(rowid), changes: int64(changes)}, nil
}

Expand Down
35 changes: 35 additions & 0 deletions sqlite3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2101,6 +2101,7 @@ var tests = []testing.InternalTest{
{Name: "TestManyQueryRow", F: testManyQueryRow},
{Name: "TestTxQuery", F: testTxQuery},
{Name: "TestPreparedStmt", F: testPreparedStmt},
{Name: "TestExecStmtDoesNotBlockTable", F: testExecStmtDoesNotBlockTable},
{Name: "TestExecEmptyQuery", F: testExecEmptyQuery},
}

Expand Down Expand Up @@ -2432,6 +2433,40 @@ func testPreparedStmt(t *testing.T) {
wg.Wait()
}

// testExecStmtDoesNotBlockTable tests that the tables' locks are cleared after
// executing a statement.
func testExecStmtDoesNotBlockTable(t *testing.T) {
db.tearDown()
db.mustExec("CREATE TABLE t (count INT)")
sel, err := db.Prepare("SELECT count FROM t")
if err != nil {
t.Fatalf("prepare 1: %v", err)
}
ins, err := db.Prepare(db.q("INSERT INTO t (count) VALUES (?)"))
if err != nil {
t.Fatalf("prepare 2: %v", err)
}
drop, err := db.Prepare(db.q("DROP TABLE t"))
if err != nil {
t.Fatalf("prepare 3: %v", err)
}

for n := 1; n <= 3; n++ {
if _, err := ins.Exec(n); err != nil {
t.Fatalf("insert(%d) = %v", n, err)
}
}

_, err = sel.Exec()
if err != nil {
t.Fatalf("exec 1: %v", err)
}
_, err = drop.Exec()
if err != nil {
t.Fatalf("exec 2: %v", err)
}
}

// testEmptyQuery is test for validating the API in case of empty query
func testExecEmptyQuery(t *testing.T) {
db.tearDown()
Expand Down