diff --git a/GRDB/Core/DatabaseReader.swift b/GRDB/Core/DatabaseReader.swift index a851c5268..e5bd0736a 100644 --- a/GRDB/Core/DatabaseReader.swift +++ b/GRDB/Core/DatabaseReader.swift @@ -28,7 +28,47 @@ public protocol DatabaseReader: AnyObject { // MARK: - Interrupting Database Operations - // TODO: doc + /// This method causes any pending database operation to abort and return at + /// its earliest opportunity. + /// + /// It can be called from any thread. + /// + /// A call to `interrupt()` that occurs when there are no running SQL + /// statements is a no-op and has no effect on SQL statements that are + /// started after `interrupt()` returns. + /// + /// A database operation that is interrupted will throw a DatabaseError with + /// code SQLITE_INTERRUPT. If the interrupted SQL operation is an INSERT, + /// UPDATE, or DELETE that is inside an explicit transaction, then the + /// entire transaction will be rolled back automatically. If the rolled back + /// transaction was created by a method which wraps your database accesses, + /// such as `DatabaseWriter.write` or `Database.inTransaction`, then all + /// database accesses will throw a DatabaseError with code SQLITE_ABORT + /// until the wrapping method returns. + /// + /// For example: + /// + /// try dbQueue.write { db in + /// // interrupted: + /// try Player(...).insert(db) // throws SQLITE_INTERRUPT + /// // not executed: + /// try Player(...).insert(db) + /// } // throws SQLITE_INTERRUPT + /// + /// try dbQueue.write { db in + /// do { + /// // interrupted: + /// try Player(...).insert(db) // throws SQLITE_INTERRUPT + /// } catch { } + /// try Player(...).insert(db) // throws SQLITE_ABORT + /// } // throws SQLITE_ABORT + /// + /// try dbQueue.write { db in + /// do { + /// // interrupted: + /// try Player(...).insert(db) // throws SQLITE_INTERRUPT + /// } catch { } + /// } // throws SQLITE_ABORT func interrupt() // MARK: - Read From Database diff --git a/Tests/GRDBTests/DatabaseAbortedTransactionTests.swift b/Tests/GRDBTests/DatabaseAbortedTransactionTests.swift index 98d1d7852..ecc96ef41 100644 --- a/Tests/GRDBTests/DatabaseAbortedTransactionTests.swift +++ b/Tests/GRDBTests/DatabaseAbortedTransactionTests.swift @@ -201,6 +201,55 @@ class DatabaseAbortedTransactionTests : GRDBTestCase { try test(setup(makeDatabasePool())) } + func testWriteTransactionAbortedByInterruptDoesNotPreventRollback() throws { + func setup(_ dbWriter: T) throws -> T { + try dbWriter.write { db in + try db.execute(sql: "CREATE TABLE t(a);") + } + return dbWriter + } + func test(_ dbWriter: DatabaseWriter) throws { + let semaphore1 = DispatchSemaphore(value: 0) + let semaphore2 = DispatchSemaphore(value: 0) + + dbWriter.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in + semaphore1.signal() + semaphore2.wait() + return nil + }) + let block1 = { + try! dbWriter.writeWithoutTransaction { db in + try db.inTransaction { + do { + try db.execute(sql: "INSERT INTO t (a) VALUES (wait())") + XCTFail("Expected error") + } catch let error as DatabaseError { + XCTAssertEqual(error.resultCode, .SQLITE_INTERRUPT) + } catch { + XCTFail("Unexpected error: \(error)") + } + + XCTAssertFalse(db.isInsideTransaction) + return .rollback + } + } + } + let block2 = { + semaphore1.wait() + dbWriter.interrupt() + semaphore2.signal() + } + let blocks = [block1, block2] + DispatchQueue.concurrentPerform(iterations: blocks.count) { index in + blocks[index]() + } + } + + try test(setup(DatabaseQueue())) + try test(setup(makeDatabaseQueue())) + try test(setup(makeDatabasePool())) + } + func testTransactionAbortedByConflictPreventsFurtherDatabaseAccess() throws { func setup(_ dbWriter: T) throws -> T { try dbWriter.write { db in