Skip to content

Commit 710e5c4

Browse files
committed
chore: update document
1 parent 5103d2d commit 710e5c4

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,23 @@ Common pragmas:
6060
### Connection Pool
6161

6262
> [!WARNING]
63-
> SQLite only allows one writer at a time. By default, Go's `database/sql` opens multiple connections, which leads to `SQLITE_BUSY` errors under concurrent writes. To avoid this, limit the pool to a single connection:
63+
> SQLite only allows one writer at a time — concurrent writes will inevitably encounter `SQLITE_BUSY` ([details](https://github.com/mattn/go-sqlite3/issues/274)). This cannot be fully avoided, but can be mitigated:
64+
>
65+
> 1. Set `busy_timeout` to allow writers to wait instead of failing immediately
66+
> 2. Limit the connection pool to a single connection to reduce lock contention
67+
> 3. Enable WAL mode to allow concurrent reads while writing
6468
6569
```go
66-
db, err := gorm.Open(sqlite.Open("sqlite.db"), &gorm.Config{})
70+
dsn := "sqlite.db?_txlock=immediate&_pragma=busy_timeout(10000)&_pragma=journal_mode(WAL)"
71+
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
6772

6873
sqlDB, _ := db.DB()
6974
sqlDB.SetMaxOpenConns(1)
7075
sqlDB.SetMaxIdleConns(1)
7176
```
7277

7378
> [!NOTE]
74-
> This serializes all database access (reads and writes). If you need concurrent reads, consider using WAL mode with a separate read-only connection pool instead.
79+
> This serializes all database access (reads and writes). If you need concurrent reads, consider using WAL mode with a separate read-only connection pool instead. This approach does not work with `:memory:` databases.
7580
7681
## Testing
7782

0 commit comments

Comments
 (0)