Skip to content

Commit c7db919

Browse files
committed
Add connection pool documentation.
1 parent 476cdc5 commit c7db919

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

Documentation/Index.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- [Read-Only Databases](#read-only-databases)
1212
- [In-Memory Databases](#in-memory-databases)
1313
- [Thread-Safety](#thread-safety)
14+
- [Connection Pools](#connection-pools)
1415
- [Building Type-Safe SQL](#building-type-safe-sql)
1516
- [Expressions](#expressions)
1617
- [Compound Expressions](#compound-expressions)
@@ -251,7 +252,7 @@ Every Connection comes equipped with its own serial queue for statement executio
251252

252253
If you maintain multiple connections for a single database, consider setting a timeout (in seconds) and/or a busy handler:
253254

254-
```swift
255+
``` swift
255256
db.busyTimeout = 5
256257

257258
db.busyHandler({ tries in
@@ -265,6 +266,33 @@ db.busyHandler({ tries in
265266
> _Note:_ The default timeout is 0, so if you see `database is locked` errors, you may be trying to access the same database simultaneously from multiple connections.
266267

267268

269+
### Connection Pools
270+
271+
Connection pools use SQLite WAL mode to allow concurrent reads and writes, which can increase performance. Connection pools are created similar to connections:
272+
273+
``` swift
274+
let pool = try ConnectionPool("path/to/db.sqlite3")
275+
```
276+
277+
Writes are done inside of a readWrite block:
278+
279+
``` swift
280+
pool.readWrite { connection in
281+
try db.run(users.insert(email <- "[email protected]", name <- "Alice"))
282+
}
283+
```
284+
285+
Reads are done inside of a read block:
286+
287+
``` swift
288+
pool.read { connection in
289+
for user in try db.prepare(users) {
290+
print("id: \(user[id]), email: \(user[email]), name: \(user[name])")
291+
}
292+
}
293+
```
294+
295+
268296
## Building Type-Safe SQL
269297

270298
SQLite.swift comes with a typed expression layer that directly maps [Swift types](https://developer.apple.com/library/prerelease/ios/documentation/General/Reference/SwiftStandardLibraryReference/) to their [SQLite counterparts](https://www.sqlite.org/datatype3.html).

0 commit comments

Comments
 (0)