go-sqlite3 icon indicating copy to clipboard operation
go-sqlite3 copied to clipboard

bugfix: after executing statement table lock was not cleared

Open letFunny opened this issue 2 years ago • 3 comments

When we execute a statement, we step once and return. If the query returned more than one row, we would not have consumed everything so the underlying table would still be locked even when the execution had finished.

This change resets the statement after execution so that the locks can be cleared.

Apart from the included test, the following go program serves to reproduce the issue:

package main

import (
	"context"
	"database/sql"

	_ "github.com/mattn/go-sqlite3"
)

func main() {
	ctx := context.Background()
	sqldb, err := sql.Open("sqlite3", "file:test.db?cache=shared&mode=memory")

	stmt, err := sqldb.PrepareContext(ctx, "CREATE TABLE person (id number, name text);")
	stmt.Exec()
	stmt, err = sqldb.PrepareContext(ctx, "INSERT INTO person VALUES (1, 'Fred'), (2, 'Susan');")
	stmt.Exec()

	stmt, err = sqldb.PrepareContext(ctx, "SELECT name FROM person;")
	_, err = stmt.ExecContext(ctx)

	stmt, err = sqldb.Prepare("DROP TABLE person;")
	_, err = stmt.ExecContext(ctx)
	if err != nil {
		panic(err) // panic: database table is locked
	}
}

letFunny avatar Oct 23 '23 13:10 letFunny

@mattn Can you take a look to see if this PR makes sense? Should I create an issue along with it?

letFunny avatar Jan 11 '24 16:01 letFunny

As far as I look this changes, looks good to me. @rittneje could you please take a look?

mattn avatar Jan 11 '24 16:01 mattn

@rittneje can you take a look?

letFunny avatar Jan 29 '24 09:01 letFunny