mirror of
https://github.com/go-vikunja/vikunja.git
synced 2026-03-08 23:03:29 -05:00
fix(db): prevent SQLite "database is locked" errors under concurrent writes
Configure SQLite connections with WAL journal mode, a 5-second busy timeout, shared cache, and a max of 1 open connection. SQLite only supports a single writer at a time, so without these settings concurrent API requests (e.g. bulk task creation) would immediately fail with "database is locked" instead of waiting and retrying.
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -9,6 +9,8 @@ config.yml.sample
|
||||
!.gitea/ISSUE_TEMPLATE/config.yml
|
||||
docs/themes/
|
||||
*.db
|
||||
*.db-shm
|
||||
*.db-wal
|
||||
Run
|
||||
dist/
|
||||
cover.*
|
||||
|
||||
16
pkg/db/db.go
16
pkg/db/db.go
@@ -250,7 +250,13 @@ func initSqliteEngine() (engine *xorm.Engine, err error) {
|
||||
}
|
||||
|
||||
if path == "memory" {
|
||||
return xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
|
||||
engine, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared&_busy_timeout=5000")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
engine.SetMaxOpenConns(1)
|
||||
engine.SetMaxIdleConns(1)
|
||||
return
|
||||
}
|
||||
|
||||
// Log the resolved database path
|
||||
@@ -276,7 +282,13 @@ func initSqliteEngine() (engine *xorm.Engine, err error) {
|
||||
_ = os.Remove(path) // Remove the file to not prevent the db from creating another one
|
||||
}
|
||||
|
||||
return xorm.NewEngine("sqlite3", path)
|
||||
engine, err = xorm.NewEngine("sqlite3", path+"?cache=shared&_busy_timeout=5000&_journal_mode=WAL")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
engine.SetMaxOpenConns(1)
|
||||
engine.SetMaxIdleConns(1)
|
||||
return
|
||||
}
|
||||
|
||||
// getUserDataDir returns the platform-appropriate directory for application data
|
||||
|
||||
Reference in New Issue
Block a user