diff --git a/GRDB/Core/Concurrency/DatabaseReader.swift b/GRDB/Core/Concurrency/DatabaseReader.swift index 430910661..9ea7e006d 100644 --- a/GRDB/Core/Concurrency/DatabaseReader.swift +++ b/GRDB/Core/Concurrency/DatabaseReader.swift @@ -213,8 +213,9 @@ public protocol DatabaseReader: AnyObject, Sendable { /// later use. /// /// - parameter value: A closure which accesses the database. - /// - throws: The error thrown by `value`, or any ``DatabaseError`` that - /// would happen while establishing the database access. + /// - throws: Any ``DatabaseError`` that happens while establishing the + /// database access, or the error thrown by `value`, or + /// `CancellationError` if the task is cancelled. @available(iOS 13, macOS 10.15, tvOS 13, *) func read( _ value: sending @escaping (Database) throws -> sending T @@ -319,8 +320,9 @@ public protocol DatabaseReader: AnyObject, Sendable { /// - warning: Attempts to write in the database may succeed. /// /// - parameter value: A closure which accesses the database. - /// - throws: The error thrown by `value`, or any ``DatabaseError`` that - /// would happen while establishing the database access. + /// - throws: Any ``DatabaseError`` that happens while establishing the + /// database access, or the error thrown by `value`, or + /// `CancellationError` if the task is cancelled. @available(iOS 13, macOS 10.15, tvOS 13, *) func unsafeRead( _ value: sending @escaping (Database) throws -> sending T diff --git a/GRDB/Core/Concurrency/DatabaseWriter.swift b/GRDB/Core/Concurrency/DatabaseWriter.swift index b93181613..0df32d52b 100644 --- a/GRDB/Core/Concurrency/DatabaseWriter.swift +++ b/GRDB/Core/Concurrency/DatabaseWriter.swift @@ -128,7 +128,9 @@ public protocol DatabaseWriter: DatabaseReader { /// can see partial updates performed by the `updates` closure. /// /// - parameter updates: A closure which accesses the database. - /// - throws: The error thrown by `updates`. + /// - throws: Any ``DatabaseError`` that happens while establishing the + /// database access, or the error thrown by `updates`, or + /// `CancellationError` if the task is cancelled. @available(iOS 13, macOS 10.15, tvOS 13, *) func writeWithoutTransaction( _ updates: sending @escaping (Database) throws -> sending T @@ -212,7 +214,9 @@ public protocol DatabaseWriter: DatabaseReader { /// can see partial updates performed by the `updates` closure. /// /// - parameter updates: A closure which accesses the database. - /// - throws: The error thrown by `updates`. + /// - throws: Any ``DatabaseError`` that happens while establishing the + /// database access, or the error thrown by `updates`, or + /// `CancellationError` if the task is cancelled. @available(iOS 13, macOS 10.15, tvOS 13, *) func barrierWriteWithoutTransaction( _ updates: sending @escaping (Database) throws -> sending T @@ -643,9 +647,9 @@ extension DatabaseWriter { /// for later use. /// /// - parameter updates: A closure which accesses the database. - /// - throws: The error thrown by `updates`, or any ``DatabaseError`` that - /// would happen while establishing the database access or committing - /// the transaction. + /// - throws: Any ``DatabaseError`` that happens while establishing the + /// database access or committing the transaction, or the error thrown + /// by `updates`, or `CancellationError` if the task is cancelled. @available(iOS 13, macOS 10.15, tvOS 13, *) public func write( _ updates: sending @escaping (Database) throws -> sending T diff --git a/GRDB/Documentation.docc/Concurrency.md b/GRDB/Documentation.docc/Concurrency.md index 1b8050419..9484b6b4c 100644 --- a/GRDB/Documentation.docc/Concurrency.md +++ b/GRDB/Documentation.docc/Concurrency.md @@ -103,6 +103,8 @@ try dbQueue.write { db in Note the identical method names: `read`, `write`. The async version is only available in async Swift functions. + The `async` database access methods honor task cancellation. Once the task is cancelled, reads and writes throw `CancellationError`, and any current transaction is rollbacked. + - **Combine publishers** For example: diff --git a/GRDB/Documentation.docc/Transactions.md b/GRDB/Documentation.docc/Transactions.md index 2067a1f09..f333fe0f7 100644 --- a/GRDB/Documentation.docc/Transactions.md +++ b/GRDB/Documentation.docc/Transactions.md @@ -142,16 +142,21 @@ try dbQueue.writeWithoutTransaction { db } ``` -Transactions can't be left opened unless the ``Configuration/allowsUnsafeTransactions`` configuration flag is set: +When performing manual transaction management, take care that a transaction can not be left opened at the end of a database access: ```swift -// fatal error: A transaction has been left opened at the end of a database access +// fatal error: A transaction has been left +// opened at the end of a database access. try dbQueue.writeWithoutTransaction { db in try db.execute(sql: "BEGIN TRANSACTION") // <- no commit or rollback } ``` +Generally speaking, make sure you perform a rollback when a commit throws an error. The `async` database access methods honor task cancellation: in a cancelled task, all commits throw `CancellationError`, but rollbacks do not. + +Applications that need to perform several database accesses in a long-lived transaction can use the ``Configuration/allowsUnsafeTransactions`` configuration flag. + It is possible to ask if a transaction is currently opened: ```swift