Skip to content

Commit 6e600f0

Browse files
committed
Update "database is locked" documentation
This explains a bit more what is going on, removes the recommendation to set cache to shared (which is considered deprecated), and mentions using the WAL.
1 parent aa77c03 commit 6e600f0

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -526,17 +526,23 @@ For an example see [dinedal/go-sqlite3-extension-functions](https://github.com/d
526526

527527
- Error: `database is locked`
528528

529-
When you get a database is locked. Please use the following options.
529+
This means two different connections are trying to write to the database at
530+
the same time.
530531

531-
Add to DSN: `cache=shared`
532+
One way to fix this is to ensure your program limits the number of writers
533+
to 1, for example by using `sync.Mutex` (it's fine to be have multiple
534+
reads, even when writing).
535+
536+
Adding `_journal_mode=wal` improves the general concurrency in most cases,
537+
although it doesn't 100% eliminate locking issues:
532538

533-
Example:
534539
```go
535-
db, err := sql.Open("sqlite3", "file:locked.sqlite?cache=shared")
540+
db, err := sql.Open("sqlite3", "file:locked.sqlite?_journal_mode=wal")
536541
```
537542

538-
Second please set the database connections of the SQL package to 1.
539-
543+
To guarantee only one connection is being run at the same time yoi can set
544+
the maximum number of database connections to `1`:
545+
540546
```go
541547
db.SetMaxOpenConns(1)
542548
```

0 commit comments

Comments
 (0)