Skip to content
Merged
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
7 changes: 7 additions & 0 deletions changelog/unreleased/sqlite-dsn-pragmas.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### English

- Move the SQLite `busy_timeout` setting into the connection DSN so it is applied on every connection opened after an idle recycle or connection-level error, instead of only on the first pooled connection. Lock contention now waits up to 5 seconds before returning `SQLITE_BUSY`, matching the existing `foreign_keys` DSN fix.

### 中文

- 将 SQLite `busy_timeout` 设置下沉到连接 DSN,使其在空闲连接回收或连接级错误后新开的每一条连接上都生效,而不再只作用于首个池内连接。锁竞争时会先等待最多 5 秒再返回 `SQLITE_BUSY`,与既有的 `foreign_keys` DSN 修复保持一致。
30 changes: 13 additions & 17 deletions internal/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,21 @@ type Store struct {

// sqliteDSN turns a database path into the driver DSN.
//
// PRAGMA foreign_keys is a per-connection setting, not a property of the file.
// Running it once through db.Exec only configured whichever connection that
// Exec happened to take; every later connection database/sql opened (after an
// idle recycle or a connection-level error) started with foreign_keys=OFF, so
// the ON DELETE CASCADE clauses on request_attempts, request_usage_details and
// request_stream_diagnostics silently stopped firing. Carrying the pragma in
// the DSN makes the driver apply it at open time on every connection.
// PRAGMAs such as foreign_keys and busy_timeout are per-connection settings,
// not properties of the file. Running one through db.Exec only configured
// whichever connection that Exec happened to take; every later connection
// database/sql opened (after an idle recycle or a connection-level error)
// started with the defaults, so the ON DELETE CASCADE clauses on
// request_attempts, request_usage_details and request_stream_diagnostics
// silently stopped firing, and lock contention immediately returned
// SQLITE_BUSY instead of waiting. Carrying both pragmas in the DSN makes the
// driver apply them at open time on every connection.
func sqliteDSN(path string) string {
if strings.Contains(path, "?") {
// Already a hand-built DSN; leave it alone rather than corrupting it.
return path
}
return path + "?_foreign_keys=on"
return path + "?_foreign_keys=on&_busy_timeout=5000"
}

func OpenStore(path string) (*Store, error) {
Expand All @@ -56,15 +58,9 @@ func OpenStore(path string) (*Store, error) {
return nil, fmt.Errorf("open sqlite: %w", err)
}
db.SetMaxOpenConns(1)
// busy_timeout is a per-connection setting too, but it is re-applied below
// because a freshly opened pool connection has not run sqliteDSN's pragmas
// yet when this Exec lands on it. foreign_keys must NOT be relied on here:
// it only ever covered the one connection this Exec happened to take, so
// the pragma that actually matters lives in the DSN.
if _, err := db.Exec("PRAGMA busy_timeout = 5000;"); err != nil {
db.Close()
return nil, fmt.Errorf("configure sqlite: %w", err)
}
// foreign_keys and busy_timeout are applied per connection from sqliteDSN
// at open time; do not rely on a one-shot Exec here, which would only cover
// whichever pooled connection it happened to run on.
store := &Store{db: db}
if err := store.migrate(context.Background()); err != nil {
db.Close()
Expand Down
Loading