More documentation about transactions

This commit is contained in:
Gwendal Roué committed 2022-11-14 18:51:39 +01:00
1 parent c23b93d71b
commit 433ed36a4b
3 files changed
+29 -19

No files matched your search

+12 -4
View File
@@ -206,7 +206,9 @@ public protocol DatabaseReader: AnyObject, Sendable {
/// This method is "unsafe" because the database reader does nothing more
/// than providing a database connection. When you use this method, you
/// become responsible for the thread-safety of your application, and
/// responsible for database accesses performed by other processes.
/// responsible for database accesses performed by other processes. See
/// <doc:Concurrency#Safe-and-Unsafe-Database-Accesses> for
/// more information.
///
/// For example:
///
@@ -241,7 +243,9 @@ public protocol DatabaseReader: AnyObject, Sendable {
/// This method is "unsafe" because the database reader does nothing more
/// than providing a database connection. When you use this method, you
/// become responsible for the thread-safety of your application, and
/// responsible for database accesses performed by other processes.
/// responsible for database accesses performed by other processes. See
/// <doc:Concurrency#Safe-and-Unsafe-Database-Accesses> for
/// more information.
///
/// For example:
///
@@ -273,7 +277,9 @@ public protocol DatabaseReader: AnyObject, Sendable {
/// This method is "unsafe" because the database reader does nothing more
/// than providing a database connection. When you use this method, you
/// become responsible for the thread-safety of your application, and
/// responsible for database accesses performed by other processes.
/// responsible for database accesses performed by other processes. See
/// <doc:Concurrency#Safe-and-Unsafe-Database-Accesses> for
/// more information.
///
/// This method can be called from other database access methods. If called
/// from the dispatch queue of a current database access (read or write),
@@ -457,7 +463,9 @@ extension DatabaseReader {
/// This method is "unsafe" because the database reader does nothing more
/// than providing a database connection. When you use this method, you
/// become responsible for the thread-safety of your application, and
/// responsible for database accesses performed by other processes.
/// responsible for database accesses performed by other processes. See
/// <doc:Concurrency#Safe-and-Unsafe-Database-Accesses> for
/// more information.
///
/// For example:
///
+14 -10
View File
@@ -42,15 +42,15 @@ import Dispatch
/// - ``spawnConcurrentRead(_:)``
/// - ``DatabaseFuture``
///
/// ### Unsafe Methods
///
/// - ``unsafeReentrantWrite(_:)``
///
/// ### Observing Database Transactions
///
/// - ``add(transactionObserver:extent:)``
/// - ``remove(transactionObserver:)``
///
/// ### Unsafe Methods
///
/// - ``unsafeReentrantWrite(_:)``
///
/// ### Other Database Operations
///
/// - ``erase()-w5n7``
@@ -94,7 +94,8 @@ public protocol DatabaseWriter: DatabaseReader {
/// can see changes performed by concurrent writes or writes performed by
/// other processes: two identical requests performed by the `updates`
/// closure may not return the same value. Concurrent database accesses
/// can see partial updates performed by the `updates` closure.
/// can see partial updates performed by the `updates` closure. See
/// <doc:Concurrency#Rule-2:-Mind-your-transactions> for more information.
///
/// - parameter updates: A closure which accesses the database.
/// - throws: The error thrown by `updates`.
@@ -133,7 +134,8 @@ public protocol DatabaseWriter: DatabaseReader {
/// can see changes performed by concurrent writes or writes performed by
/// other processes: two identical requests performed by the `updates`
/// closure may not return the same value. Concurrent database accesses
/// can see partial updates performed by the `updates` closure.
/// can see partial updates performed by the `updates` closure. See
/// <doc:Concurrency#Rule-2:-Mind-your-transactions> for more information.
///
/// - parameter updates: A closure which accesses the database.
/// - throws: The error thrown by `updates`.
@@ -173,7 +175,8 @@ public protocol DatabaseWriter: DatabaseReader {
/// can see changes performed by concurrent writes or writes performed by
/// other processes: two identical requests performed by the `updates`
/// closure may not return the same value. Concurrent database accesses
/// can see partial updates performed by the `updates` closure.
/// can see partial updates performed by the `updates` closure. See
/// <doc:Concurrency#Rule-2:-Mind-your-transactions> for more information.
///
/// - parameter updates: A closure which accesses the database. Its argument
/// is a `Result` that provides the database connection, or the failure
@@ -206,7 +209,8 @@ public protocol DatabaseWriter: DatabaseReader {
/// can see changes performed by concurrent writes or writes performed by
/// other processes: two identical requests performed by the `updates`
/// closure may not return the same value. Concurrent database accesses
/// can see partial updates performed by the `updates` closure.
/// can see partial updates performed by the `updates` closure. See
/// <doc:Concurrency#Rule-2:-Mind-your-transactions> for more information.
///
/// - parameter updates: A closure which accesses the database.
func asyncWriteWithoutTransaction(_ updates: @escaping (Database) -> Void)
@@ -216,8 +220,8 @@ public protocol DatabaseWriter: DatabaseReader {
///
/// This method can be called from other database access methods. Reentrant
/// database accesses are discouraged because they muddle transaction
/// boundaries. (see <doc:Concurrency#Rule-2:-Mind-your-transactions> for
/// more information).
/// boundaries. See <doc:Concurrency#Rule-2:-Mind-your-transactions> for
/// more information.
///
/// For example:
///
+3 -5
View File
@@ -43,13 +43,11 @@ try dbQueue.write { db in
try dbQueue.read { db
// Inside a transaction
}
let observation = ValueObservation.tracking { db
// Inside a transaction
}
```
- *Why does this rule exist?* - Because GRDB and SQLite can not guess where to insert the transaction boundaries that protect the invariants of your database. This is your task.
Alternatively, you can open an explicit transaction or savepoint with ``Database/inTransaction(_:_:)`` and ``Database/inSavepoint(_:)``.
- *Why does this rule exist?* - Because GRDB and SQLite can not guess where to insert the transaction boundaries that protect the invariants of your database. This is your task. Transactions also avoid concurrency problems, as described in the <doc:Concurrency#Safe-and-Unsafe-Database-Accesses> section below.
- *Practical advice* - Take the time to identify the invariants of your database. Some of them can be enforced in the database schema itself, such as "all books must have a non-empty title", or "all books must have an author" (see <doc:DatabaseSchema>). Some invariants can only be enforced by transactions, such as "all account credits must have a matching debit", or "all authors must have at least one book".