From 433ed36a4becb5926b73aa582d89663881ebd786 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Mon, 14 Nov 2022 18:51:39 +0100 Subject: [PATCH] More documentation about transactions --- GRDB/Core/DatabaseReader.swift | 16 ++++++++++++---- GRDB/Core/DatabaseWriter.swift | 24 ++++++++++++++---------- GRDB/Documentation.docc/Concurrency.md | 8 +++----- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/GRDB/Core/DatabaseReader.swift b/GRDB/Core/DatabaseReader.swift index 666753869..ff667252c 100644 --- a/GRDB/Core/DatabaseReader.swift +++ b/GRDB/Core/DatabaseReader.swift @@ -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 + /// 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 + /// 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 + /// 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 + /// for + /// more information. /// /// For example: /// diff --git a/GRDB/Core/DatabaseWriter.swift b/GRDB/Core/DatabaseWriter.swift index c628fd372..6322e7358 100644 --- a/GRDB/Core/DatabaseWriter.swift +++ b/GRDB/Core/DatabaseWriter.swift @@ -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 + /// 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 + /// 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 + /// 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 + /// 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 for - /// more information). + /// boundaries. See for + /// more information. /// /// For example: /// diff --git a/GRDB/Documentation.docc/Concurrency.md b/GRDB/Documentation.docc/Concurrency.md index ec48c0623..fb40d98c4 100644 --- a/GRDB/Documentation.docc/Concurrency.md +++ b/GRDB/Documentation.docc/Concurrency.md @@ -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 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 ). 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".