|
| 1 | +# ``GRDB/DatabasePool`` |
| 2 | + |
| 3 | +A database connection that allows concurrent accesses to an SQLite database. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +**Open a `DatabasePool`** with the path to a database file: |
| 8 | + |
| 9 | +```swift |
| 10 | +import GRDB |
| 11 | + |
| 12 | +let dbPool = try DatabasePool(path: "/path/to/database.sqlite") |
| 13 | +``` |
| 14 | + |
| 15 | +SQLite creates the database file if it does not already exist. The connection is closed when the database queue gets deallocated. |
| 16 | + |
| 17 | +**A `DatabasePool` can be used from any thread.** The ``DatabaseWriter/write(_:)-76inz`` and ``DatabaseReader/read(_:)-3806d`` methods are synchronous, and block the current thread until your database statements are executed in a protected dispatch queue: |
| 18 | + |
| 19 | +```swift |
| 20 | +// Modify the database: |
| 21 | +try dbPool.write { db in |
| 22 | + try Player(name: "Arthur").insert(db) |
| 23 | +} |
| 24 | + |
| 25 | +// Read values: |
| 26 | +try dbPool.read { db in |
| 27 | + let players = try Player.fetchAll(db) |
| 28 | + let playerCount = try Player.fetchCount(db) |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +Database access methods can return values: |
| 33 | + |
| 34 | +```swift |
| 35 | +let playerCount = try dbPool.read { db in |
| 36 | + try Place.fetchCount(db) |
| 37 | +} |
| 38 | + |
| 39 | +let newPlayerCount = try dbPool.write { db -> Int in |
| 40 | + try Player(name: "Arthur").insert(db) |
| 41 | + return try Player.fetchCount(db) |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +The ``DatabaseWriter/write(_:)-76inz`` method wraps your database statements in a transaction that commits if and only if no error occurs. On the first unhandled error, all changes are reverted, the whole transaction is rollbacked, and the error is rethrown. |
| 46 | + |
| 47 | +When you don't need to modify the database, prefer the ``DatabaseReader/read(_:)-3806d`` method, because several threads can perform reads in parallel. |
| 48 | + |
| 49 | +When precise transaction handling is required, see <doc:Transactions>. |
| 50 | + |
| 51 | +Asynchronous database accesses are described in <doc:Concurrency>. |
| 52 | + |
| 53 | +`DatabasePool` can take snapshots of the database: see ``DatabaseSnapshot`` and ``DatabaseSnapshotPool``. |
| 54 | + |
| 55 | +`DatabasePool` can be configured with ``Configuration``. |
| 56 | + |
| 57 | +## Concurrency |
| 58 | + |
| 59 | +A `DatabasePool` creates one writer SQLite connection, and a pool of read-only SQLite connections. |
| 60 | + |
| 61 | +Unless ``Configuration/readonly``, the database is set to the [WAL mode](https://sqlite.org/wal.html). The WAL mode makes it possible for reads and writes to proceed concurrently. |
| 62 | + |
| 63 | +All write accesses are executed in a serial **writer dispatch queue**, which means that there is never more than one thread that writes in the database. |
| 64 | + |
| 65 | +All read accesses are executed in **reader dispatch queues** (one per read-only SQLite connection). Reads are generally non-blocking, unless the maximum number of concurrent reads has been reached. In this case, a read has to wait for another read to complete. That maximum number can be configured with ``Configuration/maximumReaderCount``. |
| 66 | + |
| 67 | +SQLite connections are closed when the `DatabasePool` is deallocated. |
| 68 | + |
| 69 | +`DatabasePool` inherits most of its database access methods from the ``DatabaseReader`` and ``DatabaseWriter`` protocols. It defines a few specific database access methods as well, listed below. |
| 70 | + |
| 71 | +A `DatabasePool` needs your application to follow rules in order to deliver its safety guarantees. See <doc:Concurrency> for more information. |
| 72 | + |
| 73 | +## Topics |
| 74 | + |
| 75 | +### Creating a DatabasePool |
| 76 | + |
| 77 | +- ``init(path:configuration:)`` |
| 78 | + |
| 79 | +### Accessing the Database |
| 80 | + |
| 81 | +See ``DatabaseReader`` and ``DatabaseWriter`` for more database |
| 82 | +access methods. |
| 83 | + |
| 84 | +- ``asyncConcurrentRead(_:)`` |
| 85 | +- ``writeInTransaction(_:_:)`` |
| 86 | + |
| 87 | +### Creating Database Snapshots |
| 88 | + |
| 89 | +- ``makeSnapshot()`` |
| 90 | +- ``makeSnapshotPool()`` |
| 91 | + |
| 92 | +### Managing SQLite Connections |
| 93 | + |
| 94 | +- ``invalidateReadOnlyConnections()`` |
| 95 | +- ``releaseMemory()`` |
| 96 | +- ``releaseMemoryEventually()`` |
0 commit comments