diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index b4bac1419..8d50b0dcf 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -714,6 +714,56 @@ extension Database { public func beginTransaction(_ kind: TransactionKind? = nil) throws { let kind = kind ?? configuration.defaultTransactionKind try execute("BEGIN \(kind.rawValue) TRANSACTION") + assert(isInsideTransaction) + } + + /// Begins a database transaction and take a snapshot of the last committed + /// database state. + func beginSnapshotIsolation() throws { + // https://www.sqlite.org/isolation.html + // + // > In WAL mode, SQLite exhibits "snapshot isolation". When a read + // > transaction starts, that reader continues to see an unchanging + // > "snapshot" of the database file as it existed at the moment in time + // > when the read transaction started. Any write transactions that + // > commit while the read transaction is active are still invisible to + // > the read transaction, because the reader is seeing a snapshot of + // > database file from a prior moment in time. + // + // That's exactly what we need. But what does "when read transaction + // starts" mean? + // + // http://www.sqlite.org/lang_transaction.html + // + // > Deferred [transaction] means that no locks are acquired on the + // > database until the database is first accessed. [...] Locks are not + // > acquired until the first read or write operation. [...] Because the + // > acquisition of locks is deferred until they are needed, it is + // > possible that another thread or process could create a separate + // > transaction and write to the database after the BEGIN on the + // > current thread has executed. + // + // Now that's precise enough: SQLite defers "snapshot isolation" until + // the first SELECT: + // + // Reader Writer + // BEGIN DEFERRED TRANSACTION + // UPDATE ... (1) + // Here the change (1) is visible + // SELECT ... + // UPDATE ... (2) + // Here the change (2) is not visible + // + // We thus have to perform a select that establishes the + // snapshot isolation before we release the writer queue: + // + // Reader Writer + // BEGIN DEFERRED TRANSACTION + // SELECT anything + // UPDATE ... + // Here the change is not visible by GRDB user + try beginTransaction(.deferred) + try internalCachedSelectStatement("SELECT rootpage FROM sqlite_master LIMIT 1").makeCursor().next() } /// Rollbacks a database transaction. diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 63b9eebc8..5afcee587 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -386,50 +386,6 @@ extension DatabasePool : DatabaseReader { /// happen while establishing the read access to the database. @available(*, deprecated, message: "Use concurrentRead instead") public func readFromCurrentState(_ block: @escaping (Database) -> Void) throws { - // https://www.sqlite.org/isolation.html - // - // > In WAL mode, SQLite exhibits "snapshot isolation". When a read - // > transaction starts, that reader continues to see an unchanging - // > "snapshot" of the database file as it existed at the moment in time - // > when the read transaction started. Any write transactions that - // > commit while the read transaction is active are still invisible to - // > the read transaction, because the reader is seeing a snapshot of - // > database file from a prior moment in time. - // - // That's exactly what we need. But what does "when read transaction - // starts" mean? - // - // http://www.sqlite.org/lang_transaction.html - // - // > Deferred [transaction] means that no locks are acquired on the - // > database until the database is first accessed. [...] Locks are not - // > acquired until the first read or write operation. [...] Because the - // > acquisition of locks is deferred until they are needed, it is - // > possible that another thread or process could create a separate - // > transaction and write to the database after the BEGIN on the - // > current thread has executed. - // - // Now that's precise enough: SQLite defers "snapshot isolation" until - // the first SELECT: - // - // Reader Writer - // BEGIN DEFERRED TRANSACTION - // UPDATE ... (1) - // Here the change (1) is visible - // SELECT ... - // UPDATE ... (2) - // Here the change (2) is not visible - // - // The readFromCurrentState method says that no change should be visible - // at all. We thus have to perform a select that establishes the - // snapshot isolation before we release the writer queue: - // - // Reader Writer - // BEGIN DEFERRED TRANSACTION - // SELECT anything - // UPDATE ... - // Here the change is not visible by GRDB user - // Check that we're on the writer queue... writer.execute { db in // ... and that no transaction is opened. @@ -449,9 +405,7 @@ extension DatabasePool : DatabaseReader { try readerPool.get { reader in reader.async { db in do { - try db.beginTransaction(.deferred) - assert(db.isInsideTransaction) - try db.internalCachedSelectStatement("SELECT rootpage FROM sqlite_master").makeCursor().next() + try db.beginSnapshotIsolation() } catch { readError = error semaphore.signal() // Release the writer queue and rethrow error @@ -474,50 +428,6 @@ extension DatabasePool : DatabaseReader { } public func concurrentRead(_ block: @escaping (Database) throws -> T) -> Future { - // https://www.sqlite.org/isolation.html - // - // > In WAL mode, SQLite exhibits "snapshot isolation". When a read - // > transaction starts, that reader continues to see an unchanging - // > "snapshot" of the database file as it existed at the moment in time - // > when the read transaction started. Any write transactions that - // > commit while the read transaction is active are still invisible to - // > the read transaction, because the reader is seeing a snapshot of - // > database file from a prior moment in time. - // - // That's exactly what we need. But what does "when read transaction - // starts" mean? - // - // http://www.sqlite.org/lang_transaction.html - // - // > Deferred [transaction] means that no locks are acquired on the - // > database until the database is first accessed. [...] Locks are not - // > acquired until the first read or write operation. [...] Because the - // > acquisition of locks is deferred until they are needed, it is - // > possible that another thread or process could create a separate - // > transaction and write to the database after the BEGIN on the - // > current thread has executed. - // - // Now that's precise enough: SQLite defers "snapshot isolation" until - // the first SELECT: - // - // Reader Writer - // BEGIN DEFERRED TRANSACTION - // UPDATE ... (1) - // Here the change (1) is visible - // SELECT ... - // UPDATE ... (2) - // Here the change (2) is not visible - // - // The readFromCurrentState method says that no change should be visible - // at all. We thus have to perform a select that establishes the - // snapshot isolation before we release the writer queue: - // - // Reader Writer - // BEGIN DEFERRED TRANSACTION - // SELECT anything - // UPDATE ... - // Here the change is not visible by GRDB user - // Check that we're on the writer queue... writer.execute { db in // ... and that no transaction is opened. @@ -540,12 +450,9 @@ extension DatabasePool : DatabaseReader { do { try readerPool.get { reader in reader.async { db in - // Open a deferred transaction and access the database in - // order to establish snapshot isolation. defer { _ = try? db.commit() } do { - try db.beginTransaction(.deferred) - try db.internalCachedSelectStatement("SELECT rootpage FROM sqlite_master").makeCursor().next() + try db.beginSnapshotIsolation() } catch { // Snapshot isolation failure futureResult = .failure(error) diff --git a/GRDB/Core/DatabaseSnapshot.swift b/GRDB/Core/DatabaseSnapshot.swift index 57acc606c..aa85413ca 100644 --- a/GRDB/Core/DatabaseSnapshot.swift +++ b/GRDB/Core/DatabaseSnapshot.swift @@ -31,13 +31,7 @@ public class DatabaseSnapshot : DatabaseReader { guard journalMode == "wal" else { throw DatabaseError(message: "WAL mode is not activated at path: \(path)") } - - // Establish snapshot isolation (see deinit) - try db.beginTransaction(.deferred) - - // Take snapshot - // See DatabasePool.concurrentRead for a complete discussion - try db.internalCachedSelectStatement("SELECT rootpage FROM sqlite_master").makeCursor().next() + try db.beginSnapshotIsolation() } }