diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index b9f622dbc..af826f5a3 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -192,7 +192,14 @@ extension DatabaseQueue { /// - throws: The error thrown by the block. public func read(_ block: (Database) throws -> T) throws -> T { try writer.sync { db in - try db.readOnly { try block(db) } + // The transaction guarantees snapshot isolation against eventual + // external connection. + var result: T? + try db.inTransaction(.deferred) { + result = try db.readOnly { try block(db) } + return .commit + } + return result! } } @@ -214,6 +221,9 @@ extension DatabaseQueue { public func asyncRead(_ block: @escaping (Result) -> Void) { writer.async { db in do { + // The transaction guarantees snapshot isolation against eventual + // external connection. + try db.beginTransaction(.deferred) try db.beginReadOnly() } catch { block(.failure(error)) @@ -224,6 +234,7 @@ extension DatabaseQueue { // Ignore error because we can not notify it. try? db.endReadOnly() + try? db.commit() } } @@ -236,6 +247,9 @@ extension DatabaseQueue { } do { + // The transaction guarantees snapshot isolation against eventual + // external connection. + try db.beginTransaction(.deferred) try db.beginReadOnly() } catch { block(.failure(error)) @@ -246,6 +260,7 @@ extension DatabaseQueue { // Ignore error because we can not notify it. try? db.endReadOnly() + try? db.commit() } } @@ -289,9 +304,14 @@ extension DatabaseQueue { try writer.execute { db in // ... and that no transaction is opened. GRDBPrecondition(!db.isInsideTransaction, "must not be called from inside a transaction.") - return try db.readOnly { - try block(db) + // The transaction guarantees snapshot isolation against eventual + // external connection. + var result: T? + try db.inTransaction(.deferred) { + result = try db.readOnly { try block(db) } + return .commit } + return result! } }) } @@ -306,6 +326,7 @@ extension DatabaseQueue { GRDBPrecondition(!db.isInsideTransaction, "must not be called from inside a transaction.") do { + try db.beginTransaction(.deferred) try db.beginReadOnly() } catch { block(.failure(error)) @@ -316,6 +337,7 @@ extension DatabaseQueue { // Ignore error because we can not notify it. try? db.endReadOnly() + try? db.commit() } } diff --git a/TODO.md b/TODO.md index c4967f4e4..81f694299 100644 --- a/TODO.md +++ b/TODO.md @@ -15,7 +15,9 @@ ## Features -- [ ] Paranoid: always assume there are external connections. Wrap DatabaseQueue.read in a transaction, deprecate DatabaseQueue.inDatabase (use unsafeWriteWithoutTransaction or unsafeRead instead). +- [ ] Paranoid: always assume there are external connections. + - [X] Wrap DatabaseQueue.read in a transaction + - [ ] deprecate DatabaseQueue.inDatabase (use unsafeWriteWithoutTransaction or unsafeRead instead). - [ ] GRDB 6: decoding errors - [ ] GRDB 6: conflict resolution in persistence methods - [ ] Long run edition. Use case: user edits the database (CRUD) but the application wants to commmit and the end of the editing session.