Bart Silverstrim
Bart Silverstrim

Reputation: 3625

Precautions to avoid SQLite file/database locks when concurrent goroutines try to access the database?

I'm currently working on an application where multiple goroutines may at some point try to query or update a SQLite database file concurrently (using github.com/mattn/go-sqlite3 and database/sql drivers.)

What I'm not sure of is how locking/concurrent access is controlled; does the driver take care of it, or do I need to find a way to serialize requests with a mutex or channel?

Does someone know if the driver will wait to perform operations and retry if the database file is locked for some reason, or would it throw an error and give up?

Upvotes: 3

Views: 2944

Answers (1)

John Weldon
John Weldon

Reputation: 40749

You should open a separate connection per goroutine. The driver will take care of locking and access, but you should have a different connection per goroutine.

Also, you can add some parameters to your connection opening to speed up some access:

gDb, err = sql.Open("sqlite3", "file:databaselocked.sqlite?cache=shared&mode=rwc")

See the comments on this thread in Github, especially the linked comment by the author of the package: https://github.com/mattn/go-sqlite3/issues/148#issuecomment-56764080

Upvotes: 3

Related Questions