|
10 | 10 | - [Read-Write Databases](#read-write-databases)
|
11 | 11 | - [Read-Only Databases](#read-only-databases)
|
12 | 12 | - [In-Memory Databases](#in-memory-databases)
|
13 |
| - - [A Note on Thread-Safety](#a-note-on-thread-safety) |
| 13 | + - [Thread-Safety](#thread-safety) |
14 | 14 | - [Building Type-Safe SQL](#building-type-safe-sql)
|
15 | 15 | - [Expressions](#expressions)
|
16 | 16 | - [Compound Expressions](#compound-expressions)
|
@@ -155,7 +155,7 @@ import SQLite
|
155 | 155 |
|
156 | 156 | ### Connecting to a Database
|
157 | 157 |
|
158 |
| -Database connections are established using the `Database` class. A database is initialized with a path. SQLite will attempt to create the database file if it does not already exist. |
| 158 | +Database connections are established using the `Connection` class. A connection is initialized with a path to a database. SQLite will attempt to create the database file if it does not already exist. |
159 | 159 |
|
160 | 160 | ``` swift
|
161 | 161 | let db = try Connection("path/to/db.sqlite3")
|
@@ -220,9 +220,24 @@ let db = try Connection(.Temporary)
|
220 | 220 | In-memory databases are automatically deleted when the database connection is closed.
|
221 | 221 |
|
222 | 222 |
|
223 |
| -### A Note on Thread-Safety |
| 223 | +#### Thread-Safety |
224 | 224 |
|
225 |
| -> _Note:_ Every database comes equipped with its own serial queue for statement execution and can be safely accessed across threads. Threads that open transactions and savepoints will block other threads from executing statements while the transaction is open. |
| 225 | +Every Connection comes equipped with its own serial queue for statement execution and can be safely accessed across threads. Threads that open transactions and savepoints will block other threads from executing statements while the transaction is open. |
| 226 | + |
| 227 | +If you maintain multiple connections for a single database, consider setting a timeout (in seconds) and/or a busy handler: |
| 228 | + |
| 229 | +```swift |
| 230 | +db.busyTimeout = 5 |
| 231 | +
|
| 232 | +db.busyHandler({ tries in |
| 233 | + if tries >= 3 { |
| 234 | + return false |
| 235 | + } |
| 236 | + return true |
| 237 | +}) |
| 238 | +``` |
| 239 | + |
| 240 | +> _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. |
226 | 241 |
|
227 | 242 |
|
228 | 243 | ## Building Type-Safe SQL
|
|
0 commit comments