Fix SQLite transaction deadlocks with immediate mode
- Add _txlock=immediate to SQLite connection string - This prevents deadlocks by acquiring write locks at transaction start - Multiple concurrent writers now queue properly instead of failing instantly - Resolves 'database table is locked' errors in production
This commit is contained in:
@@ -62,7 +62,11 @@ func New(cfg *config.Config, logger *logger.Logger) (*Database, error) {
|
||||
|
||||
// Add connection parameters for go-sqlite3
|
||||
// Enable WAL mode and other performance optimizations
|
||||
dsn := fmt.Sprintf("file:%s?_busy_timeout=5000&_journal_mode=WAL&_synchronous=OFF&cache=shared", dbPath)
|
||||
// Use immediate transactions to prevent deadlocks when multiple writers compete
|
||||
dsn := fmt.Sprintf(
|
||||
"file:%s?_busy_timeout=5000&_journal_mode=WAL&_synchronous=OFF&cache=shared&_txlock=immediate",
|
||||
dbPath,
|
||||
)
|
||||
db, err := sql.Open("sqlite3", dsn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open database: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user