mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
Adapt lock prevention to the journal mode
This commit is contained in:
1 parent
f604005b4c
commit
16025356bf
15 files changed
+625
-536
No files matched your search
@@ -262,9 +262,6 @@
|
||||
564F9C341F07611900877A00 /* DatabaseFunction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564F9C2C1F075DD200877A00 /* DatabaseFunction.swift */; };
|
||||
564FCE5E20F7E11B00202B90 /* DatabaseValueConversionErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564FCE5D20F7E11A00202B90 /* DatabaseValueConversionErrorTests.swift */; };
|
||||
564FCE5F20F7E11B00202B90 /* DatabaseValueConversionErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564FCE5D20F7E11A00202B90 /* DatabaseValueConversionErrorTests.swift */; };
|
||||
565050C82393D3AF00A7F660 /* DatabaseExclusiveLockPreventionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565050C12393D3AF00A7F660 /* DatabaseExclusiveLockPreventionTests.swift */; };
|
||||
565050C92393D3AF00A7F660 /* DatabaseExclusiveLockPreventionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565050C12393D3AF00A7F660 /* DatabaseExclusiveLockPreventionTests.swift */; };
|
||||
565050CA2393D3AF00A7F660 /* DatabaseExclusiveLockPreventionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565050C12393D3AF00A7F660 /* DatabaseExclusiveLockPreventionTests.swift */; };
|
||||
5653EAD620944B4F00F46237 /* AssociationChainRowScopesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5653EAC620944B4C00F46237 /* AssociationChainRowScopesTests.swift */; };
|
||||
5653EAD720944B4F00F46237 /* AssociationChainRowScopesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5653EAC620944B4C00F46237 /* AssociationChainRowScopesTests.swift */; };
|
||||
5653EAD820944B4F00F46237 /* AssociationBelongsToRowScopeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5653EAC720944B4C00F46237 /* AssociationBelongsToRowScopeTests.swift */; };
|
||||
@@ -497,6 +494,9 @@
|
||||
567F45AC1F888B2600030B59 /* TruncateOptimizationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567F45A71F888B2600030B59 /* TruncateOptimizationTests.swift */; };
|
||||
568068311EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568068301EBBA26100EFB8AA /* SQLRequestTests.swift */; };
|
||||
568068351EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568068301EBBA26100EFB8AA /* SQLRequestTests.swift */; };
|
||||
5682D721239582AA004B58C4 /* DatabaseLockPreventionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5682D71A239582AA004B58C4 /* DatabaseLockPreventionTests.swift */; };
|
||||
5682D722239582AA004B58C4 /* DatabaseLockPreventionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5682D71A239582AA004B58C4 /* DatabaseLockPreventionTests.swift */; };
|
||||
5682D723239582AA004B58C4 /* DatabaseLockPreventionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5682D71A239582AA004B58C4 /* DatabaseLockPreventionTests.swift */; };
|
||||
568735A21CEDE16C009B9116 /* Betty.jpeg in Resources */ = {isa = PBXBuildFile; fileRef = 5687359E1CEDE16C009B9116 /* Betty.jpeg */; };
|
||||
568D131F2207213E00674B58 /* SQLQueryGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568D13182207213E00674B58 /* SQLQueryGenerator.swift */; };
|
||||
568D13202207213E00674B58 /* SQLQueryGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568D13182207213E00674B58 /* SQLQueryGenerator.swift */; };
|
||||
@@ -1410,7 +1410,6 @@
|
||||
564F9C1D1F069B4E00877A00 /* DatabaseAggregateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseAggregateTests.swift; sourceTree = "<group>"; };
|
||||
564F9C2C1F075DD200877A00 /* DatabaseFunction.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseFunction.swift; sourceTree = "<group>"; };
|
||||
564FCE5D20F7E11A00202B90 /* DatabaseValueConversionErrorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseValueConversionErrorTests.swift; sourceTree = "<group>"; };
|
||||
565050C12393D3AF00A7F660 /* DatabaseExclusiveLockPreventionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseExclusiveLockPreventionTests.swift; sourceTree = "<group>"; };
|
||||
5653EAC620944B4C00F46237 /* AssociationChainRowScopesTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationChainRowScopesTests.swift; sourceTree = "<group>"; };
|
||||
5653EAC720944B4C00F46237 /* AssociationBelongsToRowScopeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationBelongsToRowScopeTests.swift; sourceTree = "<group>"; };
|
||||
5653EAC820944B4D00F46237 /* AssociationRowScopeSearchTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationRowScopeSearchTests.swift; sourceTree = "<group>"; };
|
||||
@@ -1501,6 +1500,7 @@
|
||||
567F0B2C220F0E2E00D111FB /* SQLInterpolationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SQLInterpolationTests.swift; sourceTree = "<group>"; };
|
||||
567F45A71F888B2600030B59 /* TruncateOptimizationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TruncateOptimizationTests.swift; sourceTree = "<group>"; };
|
||||
568068301EBBA26100EFB8AA /* SQLRequestTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLRequestTests.swift; sourceTree = "<group>"; };
|
||||
5682D71A239582AA004B58C4 /* DatabaseLockPreventionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseLockPreventionTests.swift; sourceTree = "<group>"; };
|
||||
5687359E1CEDE16C009B9116 /* Betty.jpeg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = Betty.jpeg; sourceTree = "<group>"; };
|
||||
568D13182207213E00674B58 /* SQLQueryGenerator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLQueryGenerator.swift; sourceTree = "<group>"; };
|
||||
5690AFD72120589A001530EA /* InsertRecordCodableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InsertRecordCodableTests.swift; sourceTree = "<group>"; };
|
||||
@@ -2232,8 +2232,8 @@
|
||||
564A50C61BFF4B7F00B3A3A2 /* DatabaseCollationTests.swift */,
|
||||
563DE4EC231A91E2005081B7 /* DatabaseConfigurationTests.swift */,
|
||||
56A238161B9C74A90082EB20 /* DatabaseErrorTests.swift */,
|
||||
565050C12393D3AF00A7F660 /* DatabaseExclusiveLockPreventionTests.swift */,
|
||||
560C97C61BFD0B8400BF8471 /* DatabaseFunctionTests.swift */,
|
||||
5682D71A239582AA004B58C4 /* DatabaseLockPreventionTests.swift */,
|
||||
567DAF341EAB789800FC0928 /* DatabaseLogErrorTests.swift */,
|
||||
560A37A91C90084600949E71 /* DatabasePool */,
|
||||
563363BB1C93FD32000BE133 /* DatabaseQueue */,
|
||||
@@ -3491,6 +3491,7 @@
|
||||
56EA869F1C932597002BB4DF /* DatabasePoolReadOnlyTests.swift in Sources */,
|
||||
5615B288222B17C000061C1C /* AssociationHasOneThroughDecodableRecordTests.swift in Sources */,
|
||||
567F45AC1F888B2600030B59 /* TruncateOptimizationTests.swift in Sources */,
|
||||
5682D722239582AA004B58C4 /* DatabaseLockPreventionTests.swift in Sources */,
|
||||
5657AB621D108BA9006283EF /* FoundationNSURLTests.swift in Sources */,
|
||||
5657AB6A1D108BA9006283EF /* FoundationURLTests.swift in Sources */,
|
||||
563B06C42185D29F00B38F35 /* ValueObservationExtentTests.swift in Sources */,
|
||||
@@ -3585,7 +3586,6 @@
|
||||
568068351EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */,
|
||||
565D5D751BBC70AE00DC9BD4 /* StatementArguments+FoundationTests.swift in Sources */,
|
||||
56703298212B5450007D270F /* DatabaseUUIDEncodingStrategyTests.swift in Sources */,
|
||||
565050C92393D3AF00A7F660 /* DatabaseExclusiveLockPreventionTests.swift in Sources */,
|
||||
5676FBA722F5CAD9004717D9 /* ValueObservationRegionRecordingTests.swift in Sources */,
|
||||
5623931C1DECC02000A6B01F /* RowFetchTests.swift in Sources */,
|
||||
5653EAD720944B4F00F46237 /* AssociationChainRowScopesTests.swift in Sources */,
|
||||
@@ -3698,6 +3698,7 @@
|
||||
56176C591EACCCC7000F3F2B /* FTS5CustomTokenizerTests.swift in Sources */,
|
||||
5615B289222B17C000061C1C /* AssociationHasOneThroughDecodableRecordTests.swift in Sources */,
|
||||
56D4968A1D81316E008276D7 /* DatabaseValueConvertibleSubclassTests.swift in Sources */,
|
||||
5682D721239582AA004B58C4 /* DatabaseLockPreventionTests.swift in Sources */,
|
||||
56D496601D81304E008276D7 /* FoundationNSUUIDTests.swift in Sources */,
|
||||
567F45A81F888B2600030B59 /* TruncateOptimizationTests.swift in Sources */,
|
||||
563B06C32185D29F00B38F35 /* ValueObservationExtentTests.swift in Sources */,
|
||||
@@ -3792,7 +3793,6 @@
|
||||
5623934E1DEDFEFB00A6B01F /* EnumeratedCursorTests.swift in Sources */,
|
||||
568068311EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */,
|
||||
56703297212B5450007D270F /* DatabaseUUIDEncodingStrategyTests.swift in Sources */,
|
||||
565050C82393D3AF00A7F660 /* DatabaseExclusiveLockPreventionTests.swift in Sources */,
|
||||
5676FBA622F5CAD9004717D9 /* ValueObservationRegionRecordingTests.swift in Sources */,
|
||||
56D496B41D8133F8008276D7 /* DatabaseTests.swift in Sources */,
|
||||
56D496951D81317B008276D7 /* MutablePersistableRecordTests.swift in Sources */,
|
||||
@@ -4038,6 +4038,7 @@
|
||||
AAA4DD4D230F262000C74B15 /* DatabasePoolReadOnlyTests.swift in Sources */,
|
||||
AAA4DD4E230F262000C74B15 /* AssociationHasOneThroughDecodableRecordTests.swift in Sources */,
|
||||
AAA4DD4F230F262000C74B15 /* TruncateOptimizationTests.swift in Sources */,
|
||||
5682D723239582AA004B58C4 /* DatabaseLockPreventionTests.swift in Sources */,
|
||||
AAA4DD50230F262000C74B15 /* FoundationNSURLTests.swift in Sources */,
|
||||
AAA4DD51230F262000C74B15 /* FoundationURLTests.swift in Sources */,
|
||||
AAA4DD52230F262000C74B15 /* ValueObservationExtentTests.swift in Sources */,
|
||||
@@ -4132,7 +4133,6 @@
|
||||
AAA4DDA8230F262000C74B15 /* SQLRequestTests.swift in Sources */,
|
||||
AAA4DDA9230F262000C74B15 /* StatementArguments+FoundationTests.swift in Sources */,
|
||||
AAA4DDAA230F262000C74B15 /* DatabaseUUIDEncodingStrategyTests.swift in Sources */,
|
||||
565050CA2393D3AF00A7F660 /* DatabaseExclusiveLockPreventionTests.swift in Sources */,
|
||||
AAA4DDAB230F262000C74B15 /* ValueObservationRegionRecordingTests.swift in Sources */,
|
||||
AAA4DDAC230F262000C74B15 /* RowFetchTests.swift in Sources */,
|
||||
AAA4DDAD230F262000C74B15 /* AssociationChainRowScopesTests.swift in Sources */,
|
||||
|
||||
@@ -220,11 +220,68 @@ extension Database {
|
||||
}
|
||||
|
||||
extension Database {
|
||||
// TODO: cache
|
||||
private func journalMode() throws -> String? {
|
||||
// Don't return String.fetchOne(self, sql: "PRAGMA journal_mode"), so
|
||||
// that we don't create an infinite loop of assertLockPrevention(from:)
|
||||
let journalModeStatement = try internalStatementCache.selectStatement("PRAGMA journal_mode")
|
||||
try journalModeStatement.reset()
|
||||
defer {
|
||||
try? journalModeStatement.reset()
|
||||
}
|
||||
let code = sqlite3_step(journalModeStatement.sqliteStatement)
|
||||
guard code == SQLITE_ROW else {
|
||||
try journalModeStatement.didFail(withResultCode: code)
|
||||
}
|
||||
return String(cString: sqlite3_column_text(journalModeStatement.sqliteStatement, 0)!)
|
||||
}
|
||||
|
||||
private func assertLockPrevention(from statement: Statement) throws {
|
||||
try preventsLock.read { preventsLock in
|
||||
guard preventsLock else {
|
||||
return
|
||||
}
|
||||
|
||||
if try journalMode() == "wal" && statement.isReadonly {
|
||||
// In WAL mode, accept read-only statements:
|
||||
// - SELECT ...
|
||||
// - BEGIN DEFERRED TRANSACTION
|
||||
//
|
||||
// Those are not read-only:
|
||||
// - INSERT ...
|
||||
// - BEGIN IMMEDIATE TRANSACTION
|
||||
return
|
||||
}
|
||||
|
||||
if
|
||||
let updateStatement = statement as? UpdateStatement,
|
||||
updateStatement.leavesTransaction
|
||||
{
|
||||
// Accept statements that remove locks:
|
||||
// - COMMIT
|
||||
// - ROLLBACK
|
||||
// - ROLLBACK TRANSACTION TO SAVEPOINT
|
||||
// - RELEASE SAVEPOINT
|
||||
return
|
||||
}
|
||||
|
||||
// Attempt at releasing an eventual lock with ROLLBACk,
|
||||
// as explained in Database.startPreventingLock().
|
||||
//
|
||||
// Use sqlite3_exec instead of `try? rollback()` in order to avoid
|
||||
// an infinite loop of assertLockPrevention(from:)
|
||||
_ = sqlite3_exec(sqliteConnection, "ROLLBACK", nil, nil, nil)
|
||||
|
||||
throw DatabaseError(
|
||||
resultCode: .SQLITE_ABORT,
|
||||
message: "Can't acquire lock",
|
||||
sql: statement.sql,
|
||||
arguments: statement.arguments)
|
||||
}
|
||||
}
|
||||
|
||||
func executeUpdateStatement(_ statement: UpdateStatement) throws {
|
||||
if statement.createsExclusiveLock {
|
||||
try assertExclusiveLockPrevention(from: statement)
|
||||
}
|
||||
try assertLockPrevention(from: statement)
|
||||
|
||||
// In aborted transaction, forbid all statements but statements that
|
||||
// manage transactions.
|
||||
@@ -300,9 +357,7 @@ extension Database {
|
||||
|
||||
@inline(__always)
|
||||
func selectStatementWillExecute(_ statement: SelectStatement) throws {
|
||||
if !statement.isReadonly {
|
||||
try assertExclusiveLockPrevention(from: statement)
|
||||
}
|
||||
try assertLockPrevention(from: statement)
|
||||
|
||||
try assertNotInsideAbortedTransactionBlock(sql: statement.sql, arguments: statement.arguments)
|
||||
|
||||
|
||||
+12
-34
@@ -179,8 +179,8 @@ public final class Database {
|
||||
return isInsideTransactionBlock && !isInsideTransaction
|
||||
}
|
||||
|
||||
// See startPreventingExclusiveLock
|
||||
var preventsExclusiveLock = ReadWriteBox<Bool>(value: false)
|
||||
// See startPreventingLock
|
||||
var preventsLock = LockedBox<Bool>(value: false)
|
||||
|
||||
// MARK: - Private properties
|
||||
|
||||
@@ -676,24 +676,24 @@ extension Database {
|
||||
sqlite3_interrupt(sqliteConnection)
|
||||
}
|
||||
|
||||
/// Aborts any exclusive lock, as soon as possible, at any cost.
|
||||
/// Release any database lock, as soon as possible, at any cost.
|
||||
///
|
||||
/// See https://www.sqlite.org/lockingv3.html
|
||||
func startPreventingExclusiveLock() {
|
||||
preventsExclusiveLock.write { preventsExclusiveLock in
|
||||
if preventsExclusiveLock {
|
||||
func startPreventingLock() {
|
||||
preventsLock.write { preventsLock in
|
||||
if preventsLock {
|
||||
return
|
||||
}
|
||||
|
||||
// Prevent future lock acquisition
|
||||
preventsExclusiveLock = true
|
||||
preventsLock = true
|
||||
|
||||
// Interrupt the database because this may trigger an
|
||||
// SQLITE_INTERRUPT error which may itself abort a transaction and
|
||||
// release an exclusive lock. See https://www.sqlite.org/c3ref/interrupt.html
|
||||
// release a lock. See https://www.sqlite.org/c3ref/interrupt.html
|
||||
interrupt()
|
||||
|
||||
// What about the eventual remaining exclusive lock?
|
||||
// What about the eventual remaining lock?
|
||||
// If the database had been opened with the SQLITE_OPEN_FULLMUTEX
|
||||
// flag, we could safely execute a rollback statement:
|
||||
//
|
||||
@@ -707,35 +707,13 @@ extension Database {
|
||||
// - the rollback may fail, and a lock could remain.
|
||||
//
|
||||
// We work around this situation by issuing a rollback on next
|
||||
// database access which requires an exclusive lock,
|
||||
// database access which requires a lock,
|
||||
// in preventExclusiveLock(from:).
|
||||
}
|
||||
}
|
||||
|
||||
func stopPreventingExclusiveLock() {
|
||||
preventsExclusiveLock.value = false
|
||||
}
|
||||
|
||||
func assertExclusiveLockPrevention(from statement: Statement) throws {
|
||||
SchedulingWatchdog.assertValidQueue(self)
|
||||
|
||||
try preventsExclusiveLock.read { preventsExclusiveLock in
|
||||
if preventsExclusiveLock {
|
||||
// Attempt at releasing eventual exclusive lock with ROLLBACk,
|
||||
// as explained in Database.startPreventingExclusiveLock().
|
||||
//
|
||||
// We don't use `try? rollback()` because we're not in the
|
||||
// protected dispatch queue here due to
|
||||
// `preventsExclusiveLock.read`.
|
||||
_ = sqlite3_exec(sqliteConnection, "ROLLBACK", nil, nil, nil)
|
||||
|
||||
throw DatabaseError(
|
||||
resultCode: .SQLITE_ABORT,
|
||||
message: "Can't acquire exclusive lock",
|
||||
sql: statement.sql,
|
||||
arguments: statement.arguments)
|
||||
}
|
||||
}
|
||||
func stopPreventingLock() {
|
||||
preventsLock.value = false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -259,20 +259,22 @@ extension DatabasePool: DatabaseReader {
|
||||
readerPool.forEach { $0.interrupt() }
|
||||
}
|
||||
|
||||
public func startPreventingExclusiveLock() {
|
||||
// MARK: - Lock Prevention
|
||||
|
||||
public func startPreventingLock() {
|
||||
if configuration.readonly {
|
||||
// read-only connections can't acquire exclusive locks
|
||||
// read-only WAL connections can't acquire locks
|
||||
return
|
||||
}
|
||||
writer.startPreventingExclusiveLock()
|
||||
writer.startPreventingLock()
|
||||
}
|
||||
|
||||
public func stopPreventingExclusiveLock() {
|
||||
public func stopPreventingLock() {
|
||||
if configuration.readonly {
|
||||
// read-only connections can't acquire exclusive locks
|
||||
// read-only WAL connections can't acquire locks
|
||||
return
|
||||
}
|
||||
writer.stopPreventingExclusiveLock()
|
||||
writer.stopPreventingLock()
|
||||
}
|
||||
|
||||
// MARK: - Reading from Database
|
||||
|
||||
@@ -155,20 +155,14 @@ extension DatabaseQueue {
|
||||
writer.interrupt()
|
||||
}
|
||||
|
||||
public func startPreventingExclusiveLock() {
|
||||
if configuration.readonly {
|
||||
// read-only connections can't acquire exclusive locks
|
||||
return
|
||||
}
|
||||
writer.startPreventingExclusiveLock()
|
||||
// MARK: - Lock Prevention
|
||||
|
||||
public func startPreventingLock() {
|
||||
writer.startPreventingLock()
|
||||
}
|
||||
|
||||
public func stopPreventingExclusiveLock() {
|
||||
if configuration.readonly {
|
||||
// read-only connections can't acquire exclusive locks
|
||||
return
|
||||
}
|
||||
writer.stopPreventingExclusiveLock()
|
||||
public func stopPreventingLock() {
|
||||
writer.stopPreventingLock()
|
||||
}
|
||||
|
||||
// MARK: - Reading from Database
|
||||
|
||||
@@ -84,6 +84,26 @@ public protocol DatabaseReader: AnyObject {
|
||||
/// }
|
||||
func interrupt()
|
||||
|
||||
// MARK: - Lock Prevention
|
||||
|
||||
/// Starts preventing database locks.
|
||||
///
|
||||
/// This method can be called from any thread.
|
||||
///
|
||||
/// During lock prevention, any lock is released as soon as possible, and
|
||||
/// lock acquisition is prevented.
|
||||
///
|
||||
/// All database accesses may throw a DatabaseError of code
|
||||
/// `SQLITE_INTERRUPT`, or `SQLITE_ABORT`.
|
||||
///
|
||||
/// Lock prevention ends with stopPreventingLock().
|
||||
func startPreventingLock()
|
||||
|
||||
/// Ends lock prevention. See startPreventingLock().
|
||||
///
|
||||
/// This method can be called from any thread.
|
||||
func stopPreventingLock()
|
||||
|
||||
// MARK: - Read From Database
|
||||
|
||||
/// Synchronously executes a read-only block that takes a database
|
||||
@@ -315,6 +335,18 @@ public final class AnyDatabaseReader: DatabaseReader {
|
||||
base.interrupt()
|
||||
}
|
||||
|
||||
// MARK: - Lock Prevention
|
||||
|
||||
/// :nodoc:
|
||||
public func startPreventingLock() {
|
||||
base.startPreventingLock()
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
public func stopPreventingLock() {
|
||||
base.stopPreventingLock()
|
||||
}
|
||||
|
||||
// MARK: - Reading from Database
|
||||
|
||||
/// :nodoc:
|
||||
|
||||
@@ -53,6 +53,16 @@ extension DatabaseSnapshot {
|
||||
serializedDatabase.interrupt()
|
||||
}
|
||||
|
||||
// MARK: - Lock Prevention
|
||||
|
||||
public func startPreventingLock() {
|
||||
// read-only WAL connections can't acquire locks
|
||||
}
|
||||
|
||||
public func stopPreventingLock() {
|
||||
// read-only WAL connections can't acquire locks
|
||||
}
|
||||
|
||||
// MARK: - Reading from Database
|
||||
|
||||
/// Synchronously executes a read-only block that takes a database
|
||||
|
||||
@@ -174,29 +174,6 @@ public protocol DatabaseWriter: DatabaseReader {
|
||||
/// :nodoc:
|
||||
func spawnConcurrentRead(_ block: @escaping (Result<Database, Error>) -> Void)
|
||||
#endif
|
||||
|
||||
// MARK: - Exclusive Lock Prevention
|
||||
|
||||
/// Starts exclusive lock prevention.
|
||||
///
|
||||
/// This method can be called from any thread.
|
||||
///
|
||||
/// During exclusive lock prevention, any exclusive lock is released as soon
|
||||
/// as possible, and acquisition of exclusive lock is prevented.
|
||||
///
|
||||
/// All database accesses may throw a DatabaseError of code
|
||||
/// `SQLITE_INTERRUPT`, or `SQLITE_ABORT`.
|
||||
///
|
||||
/// Read-only connections are not affected. Such errors will never happen to
|
||||
/// `DatabasePool.read(_:)`, for example.
|
||||
///
|
||||
/// Exclusive lock prevention ends with stopPreventingExclusiveLock().
|
||||
func startPreventingExclusiveLock()
|
||||
|
||||
/// Ends exclusive lock prevention. See startPreventingExclusiveLock().
|
||||
///
|
||||
/// This method can be called from any thread.
|
||||
func stopPreventingExclusiveLock()
|
||||
}
|
||||
|
||||
extension DatabaseWriter {
|
||||
@@ -631,16 +608,16 @@ public final class AnyDatabaseWriter: DatabaseWriter {
|
||||
base.interrupt()
|
||||
}
|
||||
|
||||
// MARK: - Exclusive Lock Prevention
|
||||
// MARK: - Lock Prevention
|
||||
|
||||
/// :nodoc:
|
||||
public func startPreventingExclusiveLock() {
|
||||
base.startPreventingExclusiveLock()
|
||||
public func startPreventingLock() {
|
||||
base.startPreventingLock()
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
public func stopPreventingExclusiveLock() {
|
||||
base.stopPreventingExclusiveLock()
|
||||
public func stopPreventingLock() {
|
||||
base.stopPreventingLock()
|
||||
}
|
||||
|
||||
// MARK: - Reading from Database
|
||||
|
||||
@@ -204,14 +204,14 @@ final class SerializedDatabase {
|
||||
db.interrupt()
|
||||
}
|
||||
|
||||
func startPreventingExclusiveLock() {
|
||||
func startPreventingLock() {
|
||||
// Intentionally not scheduled in our serial queue
|
||||
db.startPreventingExclusiveLock()
|
||||
db.startPreventingLock()
|
||||
}
|
||||
|
||||
func stopPreventingExclusiveLock() {
|
||||
func stopPreventingLock() {
|
||||
// Intentionally not scheduled in our serial queue
|
||||
db.stopPreventingExclusiveLock()
|
||||
db.stopPreventingLock()
|
||||
}
|
||||
|
||||
/// Fatal error if current dispatch queue is not valid.
|
||||
|
||||
@@ -453,16 +453,15 @@ public final class UpdateStatement: Statement {
|
||||
private(set) var transactionEffect: TransactionEffect?
|
||||
private(set) var databaseEventKinds: [DatabaseEventKind] = []
|
||||
|
||||
var createsExclusiveLock: Bool {
|
||||
var leavesTransaction: Bool {
|
||||
switch transactionEffect {
|
||||
case .beginTransaction:
|
||||
// Not technically correct, because only immediate transactions
|
||||
// acquire an exclusive lock. But SQLite authorizer does not allow
|
||||
// us to distinguish BEGIN DEFERRED TRANSACTION from
|
||||
// BEGIN IMMEDIATE TRANSACTION.
|
||||
case .commitTransaction, .rollbackTransaction,
|
||||
.releaseSavepoint, .rollbackSavepoint:
|
||||
// Not technically correct: ROLLBACK TRANSACTION TO SAVEPOINT and
|
||||
// RELEASE SAVEPOINT do not leave transactions.
|
||||
return true
|
||||
default:
|
||||
return sqlite3_stmt_readonly(sqliteStatement) == 0
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -131,8 +131,8 @@
|
||||
564F9C251F069B4E00877A00 /* DatabaseAggregateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564F9C1D1F069B4E00877A00 /* DatabaseAggregateTests.swift */; };
|
||||
564F9C301F07611500877A00 /* DatabaseFunction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564F9C2C1F075DD200877A00 /* DatabaseFunction.swift */; };
|
||||
564F9C331F07611800877A00 /* DatabaseFunction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564F9C2C1F075DD200877A00 /* DatabaseFunction.swift */; };
|
||||
565050E123940CE400A7F660 /* DatabaseExclusiveLockPreventionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565050DF23940CE400A7F660 /* DatabaseExclusiveLockPreventionTests.swift */; };
|
||||
565050E223940CE400A7F660 /* DatabaseExclusiveLockPreventionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565050DF23940CE400A7F660 /* DatabaseExclusiveLockPreventionTests.swift */; };
|
||||
565050E123940CE400A7F660 /* DatabaseLockPreventionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565050DF23940CE400A7F660 /* DatabaseLockPreventionTests.swift */; };
|
||||
565050E223940CE400A7F660 /* DatabaseLockPreventionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565050DF23940CE400A7F660 /* DatabaseLockPreventionTests.swift */; };
|
||||
5653EB6820961FB200F46237 /* AssociationParallelSQLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5653EB5820961FB000F46237 /* AssociationParallelSQLTests.swift */; };
|
||||
5653EB6920961FB200F46237 /* AssociationParallelSQLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5653EB5820961FB000F46237 /* AssociationParallelSQLTests.swift */; };
|
||||
5653EB6A20961FB200F46237 /* AssociationBelongsToFetchableRecordTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5653EB5920961FB100F46237 /* AssociationBelongsToFetchableRecordTests.swift */; };
|
||||
@@ -809,7 +809,7 @@
|
||||
564E73E7203DA278000C443C /* JoinSupportTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JoinSupportTests.swift; sourceTree = "<group>"; };
|
||||
564F9C1D1F069B4E00877A00 /* DatabaseAggregateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseAggregateTests.swift; sourceTree = "<group>"; };
|
||||
564F9C2C1F075DD200877A00 /* DatabaseFunction.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseFunction.swift; sourceTree = "<group>"; };
|
||||
565050DF23940CE400A7F660 /* DatabaseExclusiveLockPreventionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseExclusiveLockPreventionTests.swift; sourceTree = "<group>"; };
|
||||
565050DF23940CE400A7F660 /* DatabaseLockPreventionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseLockPreventionTests.swift; sourceTree = "<group>"; };
|
||||
5653EB5820961FB000F46237 /* AssociationParallelSQLTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationParallelSQLTests.swift; sourceTree = "<group>"; };
|
||||
5653EB5920961FB100F46237 /* AssociationBelongsToFetchableRecordTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationBelongsToFetchableRecordTests.swift; sourceTree = "<group>"; };
|
||||
5653EB5A20961FB100F46237 /* AssociationParallelDecodableRecordTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationParallelDecodableRecordTests.swift; sourceTree = "<group>"; };
|
||||
@@ -1560,8 +1560,8 @@
|
||||
564A50C61BFF4B7F00B3A3A2 /* DatabaseCollationTests.swift */,
|
||||
563DE4F6231A91F6005081B7 /* DatabaseConfigurationTests.swift */,
|
||||
56A238161B9C74A90082EB20 /* DatabaseErrorTests.swift */,
|
||||
565050DF23940CE400A7F660 /* DatabaseExclusiveLockPreventionTests.swift */,
|
||||
560C97C61BFD0B8400BF8471 /* DatabaseFunctionTests.swift */,
|
||||
565050DF23940CE400A7F660 /* DatabaseLockPreventionTests.swift */,
|
||||
567DAF341EAB789800FC0928 /* DatabaseLogErrorTests.swift */,
|
||||
560A37A91C90084600949E71 /* DatabasePool */,
|
||||
563363BB1C93FD32000BE133 /* DatabaseQueue */,
|
||||
@@ -2350,7 +2350,7 @@
|
||||
6340BF871E5E3F7900832805 /* RecordPersistenceConflictPolicy.swift in Sources */,
|
||||
56057C662291C7E500A7CB10 /* AssociationHasManyThroughRowScopeTests.swift in Sources */,
|
||||
56AF74721D41FB9C005E9FF3 /* DatabaseValueConvertibleEscapingTests.swift in Sources */,
|
||||
565050E123940CE400A7F660 /* DatabaseExclusiveLockPreventionTests.swift in Sources */,
|
||||
565050E123940CE400A7F660 /* DatabaseLockPreventionTests.swift in Sources */,
|
||||
F3BA80D21CFB2FF3003DC1BA /* PersistableRecordTests.swift in Sources */,
|
||||
5656A7F722946B1B001FF3FF /* ValueObservationQueryInterfaceRequestTests.swift in Sources */,
|
||||
5615B259222AE19100061C1C /* AssociationHasOneThroughSQLDerivationTests.swift in Sources */,
|
||||
@@ -2690,7 +2690,7 @@
|
||||
F3BA80EC1CFB3017003DC1BA /* RowCopiedFromStatementTests.swift in Sources */,
|
||||
56057C652291C7E500A7CB10 /* AssociationHasManyThroughRowScopeTests.swift in Sources */,
|
||||
F3BA80D41CFB2FF4003DC1BA /* PersistableRecordTests.swift in Sources */,
|
||||
565050E223940CE400A7F660 /* DatabaseExclusiveLockPreventionTests.swift in Sources */,
|
||||
565050E223940CE400A7F660 /* DatabaseLockPreventionTests.swift in Sources */,
|
||||
6340BF831E5E3F7900832805 /* RecordPersistenceConflictPolicy.swift in Sources */,
|
||||
5656A7F622946B1B001FF3FF /* ValueObservationQueryInterfaceRequestTests.swift in Sources */,
|
||||
5615B258222AE19100061C1C /* AssociationHasOneThroughSQLDerivationTests.swift in Sources */,
|
||||
|
||||
@@ -389,12 +389,12 @@
|
||||
564A214C226B8E18001F64F1 /* NumericOverflowTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564A1F2E226B89CF001F64F1 /* NumericOverflowTests.swift */; };
|
||||
564A214D226B8E18001F64F1 /* RecordMinimalPrimaryKeySingleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564A1FAB226B89DC001F64F1 /* RecordMinimalPrimaryKeySingleTests.swift */; };
|
||||
564A2151226B8E18001F64F1 /* Betty.jpeg in Resources */ = {isa = PBXBuildFile; fileRef = 564A1F6F226B89D6001F64F1 /* Betty.jpeg */; };
|
||||
565050E723940DEF00A7F660 /* DatabaseExclusiveLockPreventionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565050E623940DEF00A7F660 /* DatabaseExclusiveLockPreventionTests.swift */; };
|
||||
565050E823940DEF00A7F660 /* DatabaseExclusiveLockPreventionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565050E623940DEF00A7F660 /* DatabaseExclusiveLockPreventionTests.swift */; };
|
||||
5656A802229474DD001FF3FF /* ValueObservationQueryInterfaceRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5656A801229474DC001FF3FF /* ValueObservationQueryInterfaceRequestTests.swift */; };
|
||||
5656A803229474DD001FF3FF /* ValueObservationQueryInterfaceRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5656A801229474DC001FF3FF /* ValueObservationQueryInterfaceRequestTests.swift */; };
|
||||
5676FBB022F5CF04004717D9 /* ValueObservationRegionRecordingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5676FBAF22F5CF04004717D9 /* ValueObservationRegionRecordingTests.swift */; };
|
||||
5676FBB122F5CF04004717D9 /* ValueObservationRegionRecordingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5676FBAF22F5CF04004717D9 /* ValueObservationRegionRecordingTests.swift */; };
|
||||
5682D725239582C6004B58C4 /* DatabaseLockPreventionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5682D724239582C6004B58C4 /* DatabaseLockPreventionTests.swift */; };
|
||||
5682D726239582C6004B58C4 /* DatabaseLockPreventionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5682D724239582C6004B58C4 /* DatabaseLockPreventionTests.swift */; };
|
||||
56915793231C0D6A00E1D237 /* PoolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56915792231C0D6A00E1D237 /* PoolTests.swift */; };
|
||||
56915794231C0D6A00E1D237 /* PoolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56915792231C0D6A00E1D237 /* PoolTests.swift */; };
|
||||
569BBA31228DF91000478429 /* AssociationPrefetchingFetchableRecordTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569BBA30228DF91000478429 /* AssociationPrefetchingFetchableRecordTests.swift */; };
|
||||
@@ -608,9 +608,9 @@
|
||||
564A1FDC226B89E1001F64F1 /* AssociationParallelSQLTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationParallelSQLTests.swift; sourceTree = "<group>"; };
|
||||
564A1FDD226B89E1001F64F1 /* FTS5RecordTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FTS5RecordTests.swift; sourceTree = "<group>"; };
|
||||
564A2156226B8E18001F64F1 /* GRDBTestsEncrypted.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GRDBTestsEncrypted.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
565050E623940DEF00A7F660 /* DatabaseExclusiveLockPreventionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseExclusiveLockPreventionTests.swift; sourceTree = "<group>"; };
|
||||
5656A801229474DC001FF3FF /* ValueObservationQueryInterfaceRequestTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueObservationQueryInterfaceRequestTests.swift; sourceTree = "<group>"; };
|
||||
5676FBAF22F5CF04004717D9 /* ValueObservationRegionRecordingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueObservationRegionRecordingTests.swift; sourceTree = "<group>"; };
|
||||
5682D724239582C6004B58C4 /* DatabaseLockPreventionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseLockPreventionTests.swift; sourceTree = "<group>"; };
|
||||
56915792231C0D6A00E1D237 /* PoolTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PoolTests.swift; sourceTree = "<group>"; };
|
||||
569BBA30228DF91000478429 /* AssociationPrefetchingFetchableRecordTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationPrefetchingFetchableRecordTests.swift; sourceTree = "<group>"; };
|
||||
56DF0025228DE00900D611F3 /* AssociationPrefetchingCodableRecordTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationPrefetchingCodableRecordTests.swift; sourceTree = "<group>"; };
|
||||
@@ -714,8 +714,8 @@
|
||||
564A1F83226B89D8001F64F1 /* DatabaseDateDecodingStrategyTests.swift */,
|
||||
564A1F54226B89D2001F64F1 /* DatabaseDateEncodingStrategyTests.swift */,
|
||||
564A1F65226B89D5001F64F1 /* DatabaseErrorTests.swift */,
|
||||
565050E623940DEF00A7F660 /* DatabaseExclusiveLockPreventionTests.swift */,
|
||||
564A1F37226B89CF001F64F1 /* DatabaseFunctionTests.swift */,
|
||||
5682D724239582C6004B58C4 /* DatabaseLockPreventionTests.swift */,
|
||||
564A1F5C226B89D3001F64F1 /* DatabaseLogErrorTests.swift */,
|
||||
564A1FD4226B89E0001F64F1 /* DatabaseMigratorTests.swift */,
|
||||
564A1F6A226B89D5001F64F1 /* DatabasePoolBackupTests.swift */,
|
||||
@@ -1170,7 +1170,6 @@
|
||||
564A2045226B89E1001F64F1 /* FetchableRecordDecodableTests.swift in Sources */,
|
||||
564A1FF4226B89E1001F64F1 /* DatabasePoolCollationTests.swift in Sources */,
|
||||
564A2025226B89E1001F64F1 /* QueryInterfaceExtensibilityTests.swift in Sources */,
|
||||
565050E723940DEF00A7F660 /* DatabaseExclusiveLockPreventionTests.swift in Sources */,
|
||||
564A2038226B89E1001F64F1 /* RowFromDictionaryTests.swift in Sources */,
|
||||
564A200B226B89E1001F64F1 /* DatabaseDateEncodingStrategyTests.swift in Sources */,
|
||||
564A2070226B89E1001F64F1 /* ValueObservationCompactMapTests.swift in Sources */,
|
||||
@@ -1252,6 +1251,7 @@
|
||||
564A2027226B89E1001F64F1 /* AssociationParallelDecodableRecordTests.swift in Sources */,
|
||||
564A1FEC226B89E1001F64F1 /* AssociationBelongsToDecodableRecordTests.swift in Sources */,
|
||||
564A208B226B89E1001F64F1 /* DatabaseMigratorTests.swift in Sources */,
|
||||
5682D725239582C6004B58C4 /* DatabaseLockPreventionTests.swift in Sources */,
|
||||
564A206D226B89E1001F64F1 /* ValueObservationCombineTests.swift in Sources */,
|
||||
564A2022226B89E1001F64F1 /* CursorTests.swift in Sources */,
|
||||
564A203A226B89E1001F64F1 /* DatabaseDateDecodingStrategyTests.swift in Sources */,
|
||||
@@ -1375,7 +1375,6 @@
|
||||
564A20EE226B8E18001F64F1 /* FetchableRecordDecodableTests.swift in Sources */,
|
||||
564A20EF226B8E18001F64F1 /* DatabasePoolCollationTests.swift in Sources */,
|
||||
564A20F0226B8E18001F64F1 /* QueryInterfaceExtensibilityTests.swift in Sources */,
|
||||
565050E823940DEF00A7F660 /* DatabaseExclusiveLockPreventionTests.swift in Sources */,
|
||||
564A20F1226B8E18001F64F1 /* RowFromDictionaryTests.swift in Sources */,
|
||||
564A20F2226B8E18001F64F1 /* DatabaseDateEncodingStrategyTests.swift in Sources */,
|
||||
564A20F3226B8E18001F64F1 /* ValueObservationCompactMapTests.swift in Sources */,
|
||||
@@ -1457,6 +1456,7 @@
|
||||
564A2139226B8E18001F64F1 /* AssociationParallelDecodableRecordTests.swift in Sources */,
|
||||
564A213A226B8E18001F64F1 /* AssociationBelongsToDecodableRecordTests.swift in Sources */,
|
||||
564A213B226B8E18001F64F1 /* DatabaseMigratorTests.swift in Sources */,
|
||||
5682D726239582C6004B58C4 /* DatabaseLockPreventionTests.swift in Sources */,
|
||||
564A213C226B8E18001F64F1 /* ValueObservationCombineTests.swift in Sources */,
|
||||
564A213D226B8E18001F64F1 /* CursorTests.swift in Sources */,
|
||||
564A213E226B8E18001F64F1 /* DatabaseDateDecodingStrategyTests.swift in Sources */,
|
||||
|
||||
@@ -391,12 +391,12 @@
|
||||
564A2151226B8E18001F64F1 /* Betty.jpeg in Resources */ = {isa = PBXBuildFile; fileRef = 564A1F6F226B89D6001F64F1 /* Betty.jpeg */; };
|
||||
564A2159226C8F25001F64F1 /* db.SQLCipher3 in Resources */ = {isa = PBXBuildFile; fileRef = 564A2158226C8F24001F64F1 /* db.SQLCipher3 */; };
|
||||
564A215A226C8F25001F64F1 /* db.SQLCipher3 in Resources */ = {isa = PBXBuildFile; fileRef = 564A2158226C8F24001F64F1 /* db.SQLCipher3 */; };
|
||||
565050E423940DD500A7F660 /* DatabaseExclusiveLockPreventionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565050E323940DD400A7F660 /* DatabaseExclusiveLockPreventionTests.swift */; };
|
||||
565050E523940DD500A7F660 /* DatabaseExclusiveLockPreventionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565050E323940DD400A7F660 /* DatabaseExclusiveLockPreventionTests.swift */; };
|
||||
5656A805229474F4001FF3FF /* ValueObservationQueryInterfaceRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5656A804229474F4001FF3FF /* ValueObservationQueryInterfaceRequestTests.swift */; };
|
||||
5656A806229474F4001FF3FF /* ValueObservationQueryInterfaceRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5656A804229474F4001FF3FF /* ValueObservationQueryInterfaceRequestTests.swift */; };
|
||||
5676FBAD22F5CEF6004717D9 /* ValueObservationRegionRecordingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5676FBAC22F5CEF5004717D9 /* ValueObservationRegionRecordingTests.swift */; };
|
||||
5676FBAE22F5CEF6004717D9 /* ValueObservationRegionRecordingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5676FBAC22F5CEF5004717D9 /* ValueObservationRegionRecordingTests.swift */; };
|
||||
5682D728239582E3004B58C4 /* DatabaseLockPreventionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5682D727239582E2004B58C4 /* DatabaseLockPreventionTests.swift */; };
|
||||
5682D729239582E3004B58C4 /* DatabaseLockPreventionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5682D727239582E2004B58C4 /* DatabaseLockPreventionTests.swift */; };
|
||||
56915790231C0D5100E1D237 /* PoolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5691578F231C0D5100E1D237 /* PoolTests.swift */; };
|
||||
56915791231C0D5100E1D237 /* PoolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5691578F231C0D5100E1D237 /* PoolTests.swift */; };
|
||||
569BBA2E228DF90200478429 /* AssociationPrefetchingFetchableRecordTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569BBA2D228DF90200478429 /* AssociationPrefetchingFetchableRecordTests.swift */; };
|
||||
@@ -611,9 +611,9 @@
|
||||
564A1FDD226B89E1001F64F1 /* FTS5RecordTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FTS5RecordTests.swift; sourceTree = "<group>"; };
|
||||
564A2156226B8E18001F64F1 /* GRDBTestsEncrypted.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GRDBTestsEncrypted.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
564A2158226C8F24001F64F1 /* db.SQLCipher3 */ = {isa = PBXFileReference; lastKnownFileType = file; path = db.SQLCipher3; sourceTree = SOURCE_ROOT; };
|
||||
565050E323940DD400A7F660 /* DatabaseExclusiveLockPreventionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseExclusiveLockPreventionTests.swift; sourceTree = "<group>"; };
|
||||
5656A804229474F4001FF3FF /* ValueObservationQueryInterfaceRequestTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueObservationQueryInterfaceRequestTests.swift; sourceTree = "<group>"; };
|
||||
5676FBAC22F5CEF5004717D9 /* ValueObservationRegionRecordingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueObservationRegionRecordingTests.swift; sourceTree = "<group>"; };
|
||||
5682D727239582E2004B58C4 /* DatabaseLockPreventionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseLockPreventionTests.swift; sourceTree = "<group>"; };
|
||||
5691578F231C0D5100E1D237 /* PoolTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PoolTests.swift; sourceTree = "<group>"; };
|
||||
569BBA2D228DF90200478429 /* AssociationPrefetchingFetchableRecordTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationPrefetchingFetchableRecordTests.swift; sourceTree = "<group>"; };
|
||||
56DF001F228DDFF000D611F3 /* AssociationPrefetchingRowTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationPrefetchingRowTests.swift; sourceTree = "<group>"; };
|
||||
@@ -717,8 +717,8 @@
|
||||
564A1F83226B89D8001F64F1 /* DatabaseDateDecodingStrategyTests.swift */,
|
||||
564A1F54226B89D2001F64F1 /* DatabaseDateEncodingStrategyTests.swift */,
|
||||
564A1F65226B89D5001F64F1 /* DatabaseErrorTests.swift */,
|
||||
565050E323940DD400A7F660 /* DatabaseExclusiveLockPreventionTests.swift */,
|
||||
564A1F37226B89CF001F64F1 /* DatabaseFunctionTests.swift */,
|
||||
5682D727239582E2004B58C4 /* DatabaseLockPreventionTests.swift */,
|
||||
564A1F5C226B89D3001F64F1 /* DatabaseLogErrorTests.swift */,
|
||||
564A1FD4226B89E0001F64F1 /* DatabaseMigratorTests.swift */,
|
||||
564A1F6A226B89D5001F64F1 /* DatabasePoolBackupTests.swift */,
|
||||
@@ -1176,7 +1176,6 @@
|
||||
564A2045226B89E1001F64F1 /* FetchableRecordDecodableTests.swift in Sources */,
|
||||
564A1FF4226B89E1001F64F1 /* DatabasePoolCollationTests.swift in Sources */,
|
||||
564A2025226B89E1001F64F1 /* QueryInterfaceExtensibilityTests.swift in Sources */,
|
||||
565050E423940DD500A7F660 /* DatabaseExclusiveLockPreventionTests.swift in Sources */,
|
||||
564A2038226B89E1001F64F1 /* RowFromDictionaryTests.swift in Sources */,
|
||||
564A200B226B89E1001F64F1 /* DatabaseDateEncodingStrategyTests.swift in Sources */,
|
||||
564A2070226B89E1001F64F1 /* ValueObservationCompactMapTests.swift in Sources */,
|
||||
@@ -1225,6 +1224,7 @@
|
||||
564A208E226B89E1001F64F1 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */,
|
||||
564A2029226B89E1001F64F1 /* FTS3TableBuilderTests.swift in Sources */,
|
||||
561CFAAA2376EFAD000C8BAA /* AssociationHasManyOrderingTests.swift in Sources */,
|
||||
5682D728239582E3004B58C4 /* DatabaseLockPreventionTests.swift in Sources */,
|
||||
561CFAA82376EFAD000C8BAA /* AssociationHasManyThroughOrderingTests.swift in Sources */,
|
||||
564A2001226B89E1001F64F1 /* RecordPrimaryKeyNoneTests.swift in Sources */,
|
||||
564A2051226B89E1001F64F1 /* RecordWithColumnNameManglingTests.swift in Sources */,
|
||||
@@ -1381,7 +1381,6 @@
|
||||
564A20EE226B8E18001F64F1 /* FetchableRecordDecodableTests.swift in Sources */,
|
||||
564A20EF226B8E18001F64F1 /* DatabasePoolCollationTests.swift in Sources */,
|
||||
564A20F0226B8E18001F64F1 /* QueryInterfaceExtensibilityTests.swift in Sources */,
|
||||
565050E523940DD500A7F660 /* DatabaseExclusiveLockPreventionTests.swift in Sources */,
|
||||
564A20F1226B8E18001F64F1 /* RowFromDictionaryTests.swift in Sources */,
|
||||
564A20F2226B8E18001F64F1 /* DatabaseDateEncodingStrategyTests.swift in Sources */,
|
||||
564A20F3226B8E18001F64F1 /* ValueObservationCompactMapTests.swift in Sources */,
|
||||
@@ -1430,6 +1429,7 @@
|
||||
564A211D226B8E18001F64F1 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */,
|
||||
564A211E226B8E18001F64F1 /* FTS3TableBuilderTests.swift in Sources */,
|
||||
561CFAAB2376EFAD000C8BAA /* AssociationHasManyOrderingTests.swift in Sources */,
|
||||
5682D729239582E3004B58C4 /* DatabaseLockPreventionTests.swift in Sources */,
|
||||
561CFAA92376EFAD000C8BAA /* AssociationHasManyThroughOrderingTests.swift in Sources */,
|
||||
564A211F226B8E18001F64F1 /* RecordPrimaryKeyNoneTests.swift in Sources */,
|
||||
564A2120226B8E18001F64F1 /* RecordWithColumnNameManglingTests.swift in Sources */,
|
||||
|
||||
@@ -1,413 +0,0 @@
|
||||
import XCTest
|
||||
#if GRDBCUSTOMSQLITE
|
||||
import GRDBCustomSQLite
|
||||
#else
|
||||
import GRDB
|
||||
#endif
|
||||
|
||||
class DatabaseExclusiveLockPreventionTests : GRDBTestCase {
|
||||
|
||||
func testExclusiveLockPreventionPreventsNewTransaction() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
dbQueue.startPreventingExclusiveLock()
|
||||
do {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "BEGIN TRANSACTION")
|
||||
}
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ABORT)
|
||||
XCTAssertEqual(error.message, "Can't acquire exclusive lock")
|
||||
XCTAssertEqual(error.sql, "BEGIN TRANSACTION")
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
func testExclusiveLockPreventionDoesNotPreventCommit() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "BEGIN TRANSACTION")
|
||||
dbQueue.startPreventingExclusiveLock()
|
||||
try db.execute(sql: "COMMIT")
|
||||
}
|
||||
}
|
||||
|
||||
func testExclusiveLockPreventionDoesNotPreventRollback() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "BEGIN TRANSACTION")
|
||||
dbQueue.startPreventingExclusiveLock()
|
||||
try db.execute(sql: "ROLLBACK")
|
||||
}
|
||||
}
|
||||
|
||||
func testExclusiveLockPreventionDoesNotPreventRead() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE t(a);")
|
||||
dbQueue.startPreventingExclusiveLock()
|
||||
try XCTAssertEqual(Int.fetchOne(db, sql: "SELECT COUNT(*) FROM t"), 0)
|
||||
}
|
||||
}
|
||||
|
||||
func testExclusiveLockPreventionDoesNotPreventSavepoint() throws {
|
||||
// This is because savepoints do not acquire exclusive locks.
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE t(a);")
|
||||
dbQueue.startPreventingExclusiveLock()
|
||||
try db.execute(sql: "SAVEPOINT test")
|
||||
try db.execute(sql: "RELEASE SAVEPOINT test")
|
||||
}
|
||||
}
|
||||
|
||||
func testExclusiveLockPreventionPreventsWrite() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE t(a);")
|
||||
dbQueue.startPreventingExclusiveLock()
|
||||
do {
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ABORT)
|
||||
XCTAssertEqual(error.message, "Can't acquire exclusive lock")
|
||||
XCTAssertEqual(error.sql, "INSERT INTO t DEFAULT VALUES")
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testExclusiveLockPreventionRollbacksOnPreventedWrite() throws {
|
||||
do {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.execute(sql: "CREATE TABLE t(a);")
|
||||
XCTAssertTrue(db.isInsideTransaction)
|
||||
dbQueue.startPreventingExclusiveLock()
|
||||
do {
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ABORT)
|
||||
XCTAssertEqual(error.message, "Can't acquire exclusive lock")
|
||||
XCTAssertEqual(error.sql, "INSERT INTO t DEFAULT VALUES")
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
// Aborded transaction
|
||||
XCTAssertFalse(db.isInsideTransaction)
|
||||
}
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ABORT)
|
||||
XCTAssertEqual(error.message, "Transaction was aborted")
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
|
||||
|
||||
do {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE t(a);")
|
||||
dbQueue.startPreventingExclusiveLock()
|
||||
try db.execute(sql: "SAVEPOINT test")
|
||||
do {
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ABORT)
|
||||
XCTAssertEqual(error.message, "Can't acquire exclusive lock")
|
||||
XCTAssertEqual(error.sql, "INSERT INTO t DEFAULT VALUES")
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
// Aborded transaction
|
||||
XCTAssertFalse(db.isInsideTransaction)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testExclusiveLockPreventionAbortsDatabaseQueueAccess() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
|
||||
let semaphore1 = DispatchSemaphore(value: 0)
|
||||
let semaphore2 = DispatchSemaphore(value: 0)
|
||||
dbQueue.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
|
||||
let block1 = {
|
||||
do {
|
||||
_ = try dbQueue.inDatabase {
|
||||
try Row.fetchAll($0, sql: "SELECT wait()")
|
||||
}
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_INTERRUPT)
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
}
|
||||
let block2 = {
|
||||
semaphore1.wait()
|
||||
dbQueue.startPreventingExclusiveLock()
|
||||
semaphore2.signal()
|
||||
}
|
||||
let blocks = [block1, block2]
|
||||
DispatchQueue.concurrentPerform(iterations: blocks.count) { index in
|
||||
blocks[index]()
|
||||
}
|
||||
}
|
||||
|
||||
func testExclusiveLockPreventionDoesNotAbortDatabasePoolReadTransaction() throws {
|
||||
let dbPool = try makeDatabasePool()
|
||||
|
||||
let semaphore1 = DispatchSemaphore(value: 0)
|
||||
let semaphore2 = DispatchSemaphore(value: 0)
|
||||
|
||||
dbPool.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
let block1 = {
|
||||
do {
|
||||
_ = try dbPool.read {
|
||||
try Row.fetchAll($0, sql: "SELECT wait()")
|
||||
}
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
}
|
||||
let block2 = {
|
||||
semaphore1.wait()
|
||||
dbPool.startPreventingExclusiveLock()
|
||||
semaphore2.signal()
|
||||
}
|
||||
let blocks = [block1, block2]
|
||||
DispatchQueue.concurrentPerform(iterations: blocks.count) { index in
|
||||
blocks[index]()
|
||||
}
|
||||
}
|
||||
|
||||
func testExclusiveLockPreventionDoesNotPreventFurtherDatabaseQueueRead() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
|
||||
try dbQueue.write { db in
|
||||
try db.execute(sql: "CREATE TABLE t(a);")
|
||||
}
|
||||
|
||||
let semaphore1 = DispatchSemaphore(value: 0)
|
||||
let semaphore2 = DispatchSemaphore(value: 0)
|
||||
|
||||
dbQueue.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
let block1 = {
|
||||
try! dbQueue.inDatabase { db in
|
||||
let wasInTransaction = db.isInsideTransaction
|
||||
do {
|
||||
_ = try Row.fetchAll(db, sql: "SELECT wait()")
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_INTERRUPT)
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
XCTAssertEqual(db.isInsideTransaction, wasInTransaction)
|
||||
try XCTAssertEqual(Int.fetchOne(db, sql: "SELECT COUNT(*) FROM t"), 0)
|
||||
}
|
||||
}
|
||||
let block2 = {
|
||||
semaphore1.wait()
|
||||
dbQueue.startPreventingExclusiveLock()
|
||||
semaphore2.signal()
|
||||
}
|
||||
let blocks = [block1, block2]
|
||||
DispatchQueue.concurrentPerform(iterations: blocks.count) { index in
|
||||
blocks[index]()
|
||||
}
|
||||
}
|
||||
|
||||
func testWriteTransactionAbortedDuringStatementExecution() throws {
|
||||
func setup<T: DatabaseWriter>(_ 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 = {
|
||||
do {
|
||||
try dbWriter.write { db in
|
||||
try db.execute(sql: "INSERT INTO t SELECT wait()")
|
||||
}
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
// Transactions throw the first uncatched error: SQLITE_INTERRUPT
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_INTERRUPT)
|
||||
XCTAssertEqual(error.message, "interrupted")
|
||||
XCTAssertEqual(error.sql, "INSERT INTO t SELECT wait()")
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
}
|
||||
let block2 = {
|
||||
semaphore1.wait()
|
||||
dbWriter.startPreventingExclusiveLock()
|
||||
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 testWriteTransactionAbortedDuringStatementExecutionPreventsFurtherDatabaseAccess() throws {
|
||||
func setup<T: DatabaseWriter>(_ 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 = {
|
||||
do {
|
||||
try dbWriter.write { db in
|
||||
do {
|
||||
try db.execute(sql: "INSERT INTO t SELECT wait()")
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_INTERRUPT)
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
|
||||
XCTAssertFalse(db.isInsideTransaction)
|
||||
|
||||
do {
|
||||
_ = try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM t")
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ABORT)
|
||||
XCTAssertEqual(error.message, "Transaction was aborted")
|
||||
XCTAssertEqual(error.sql, "SELECT COUNT(*) FROM t")
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
|
||||
do {
|
||||
try db.execute(sql: "INSERT INTO t (a) VALUES (0)")
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ABORT)
|
||||
XCTAssertEqual(error.message, "Can't acquire exclusive lock")
|
||||
XCTAssertEqual(error.sql, "INSERT INTO t (a) VALUES (0)")
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
}
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
// SQLITE_INTERRUPT has been caught. So we get SQLITE_ABORT
|
||||
// from the last commit.
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ABORT)
|
||||
XCTAssertEqual(error.message, "Transaction was aborted")
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
}
|
||||
let block2 = {
|
||||
semaphore1.wait()
|
||||
dbWriter.startPreventingExclusiveLock()
|
||||
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 testWriteTransactionAbortedDuringStatementExecutionDoesNotPreventRollback() throws {
|
||||
func setup<T: DatabaseWriter>(_ 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 SELECT 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.startPreventingExclusiveLock()
|
||||
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()))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,455 @@
|
||||
import XCTest
|
||||
#if GRDBCUSTOMSQLITE
|
||||
import GRDBCustomSQLite
|
||||
#else
|
||||
import GRDB
|
||||
#endif
|
||||
|
||||
class DatabaseLockPreventionTests : GRDBTestCase {
|
||||
|
||||
private func makeDatabaseQueue(journalMode: String) throws -> DatabaseQueue {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let actualJournalMode = try dbQueue.inDatabase { try String.fetchOne($0, sql: "PRAGMA journal_mode=\(journalMode)") }
|
||||
XCTAssertEqual(actualJournalMode, journalMode)
|
||||
return dbQueue
|
||||
}
|
||||
|
||||
// MARK: - BEGIN TRANSACTION
|
||||
|
||||
func testLockPreventionPreventsNewTransactionInDeleteJournalMode() throws {
|
||||
let dbQueue = try makeDatabaseQueue(journalMode: "delete")
|
||||
dbQueue.startPreventingLock()
|
||||
|
||||
do {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "BEGIN TRANSACTION")
|
||||
}
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ABORT)
|
||||
XCTAssertEqual(error.message, "Can't acquire lock")
|
||||
XCTAssertEqual(error.sql, "BEGIN TRANSACTION")
|
||||
}
|
||||
|
||||
do {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "BEGIN IMMEDIATE TRANSACTION")
|
||||
}
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ABORT)
|
||||
XCTAssertEqual(error.message, "Can't acquire lock")
|
||||
XCTAssertEqual(error.sql, "BEGIN IMMEDIATE TRANSACTION")
|
||||
}
|
||||
|
||||
do {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "BEGIN EXCLUSIVE TRANSACTION")
|
||||
}
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ABORT)
|
||||
XCTAssertEqual(error.message, "Can't acquire lock")
|
||||
XCTAssertEqual(error.sql, "BEGIN EXCLUSIVE TRANSACTION")
|
||||
}
|
||||
|
||||
do {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "SAVEPOINT test")
|
||||
}
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ABORT)
|
||||
XCTAssertEqual(error.message, "Can't acquire lock")
|
||||
XCTAssertEqual(error.sql, "SAVEPOINT test")
|
||||
}
|
||||
}
|
||||
|
||||
func testLockPreventionDoesNotPreventNewDeferredTransactionInWALMode() throws {
|
||||
let dbQueue = try makeDatabaseQueue(journalMode: "wal")
|
||||
dbQueue.startPreventingLock()
|
||||
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "BEGIN TRANSACTION; ROLLBACK")
|
||||
try db.execute(sql: "SAVEPOINT test; RELEASE SAVEPOINT test")
|
||||
}
|
||||
}
|
||||
|
||||
func testLockPreventionPreventsNewImmediateOrExclusiveTransactionInWALMode() throws {
|
||||
let dbQueue = try makeDatabaseQueue(journalMode: "wal")
|
||||
dbQueue.startPreventingLock()
|
||||
|
||||
do {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "BEGIN IMMEDIATE TRANSACTION")
|
||||
}
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ABORT)
|
||||
XCTAssertEqual(error.message, "Can't acquire lock")
|
||||
XCTAssertEqual(error.sql, "BEGIN IMMEDIATE TRANSACTION")
|
||||
}
|
||||
|
||||
do {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "BEGIN EXCLUSIVE TRANSACTION")
|
||||
}
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ABORT)
|
||||
XCTAssertEqual(error.message, "Can't acquire lock")
|
||||
XCTAssertEqual(error.sql, "BEGIN EXCLUSIVE TRANSACTION")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - COMMIT, ROLLBACK, RELEASE SAVEPOINT, ROLLBACK TRANSACTION TO SAVEPOINT
|
||||
|
||||
func testLockPreventionDoesNotPreventCommit() throws {
|
||||
func test(_ dbQueue: DatabaseQueue) throws {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "BEGIN TRANSACTION")
|
||||
dbQueue.startPreventingLock()
|
||||
try db.execute(sql: "COMMIT")
|
||||
}
|
||||
}
|
||||
try test(makeDatabaseQueue(journalMode: "delete"))
|
||||
try test(makeDatabaseQueue(journalMode: "wal"))
|
||||
}
|
||||
|
||||
func testLockPreventionDoesNotPreventRollback() throws {
|
||||
func test(_ dbQueue: DatabaseQueue) throws {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "BEGIN TRANSACTION")
|
||||
dbQueue.startPreventingLock()
|
||||
try db.execute(sql: "ROLLBACK")
|
||||
}
|
||||
}
|
||||
try test(makeDatabaseQueue(journalMode: "delete"))
|
||||
try test(makeDatabaseQueue(journalMode: "wal"))
|
||||
}
|
||||
|
||||
func testLockPreventionDoesNotPreventReleaseSavePoint() throws {
|
||||
func test(_ dbQueue: DatabaseQueue) throws {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "SAVEPOINT test")
|
||||
dbQueue.startPreventingLock()
|
||||
try db.execute(sql: "RELEASE SAVEPOINT test")
|
||||
}
|
||||
}
|
||||
try test(makeDatabaseQueue(journalMode: "delete"))
|
||||
try test(makeDatabaseQueue(journalMode: "wal"))
|
||||
}
|
||||
|
||||
func testLockPreventionDoesNotPreventRollbackSavePoint() throws {
|
||||
func test(_ dbQueue: DatabaseQueue) throws {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "SAVEPOINT test")
|
||||
dbQueue.startPreventingLock()
|
||||
try db.execute(sql: "ROLLBACK TRANSACTION TO SAVEPOINT test")
|
||||
try db.execute(sql: "RELEASE SAVEPOINT test")
|
||||
}
|
||||
}
|
||||
try test(makeDatabaseQueue(journalMode: "delete"))
|
||||
try test(makeDatabaseQueue(journalMode: "wal"))
|
||||
}
|
||||
|
||||
// MARK: - SELECT
|
||||
|
||||
func testLockPreventionPreventsReadInDeleteJournalMode() throws {
|
||||
let dbQueue = try makeDatabaseQueue(journalMode: "delete")
|
||||
do {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE t(a);")
|
||||
dbQueue.startPreventingLock()
|
||||
_ = try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM t")
|
||||
}
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ABORT)
|
||||
XCTAssertEqual(error.message, "Can't acquire lock")
|
||||
XCTAssertEqual(error.sql, "SELECT COUNT(*) FROM t")
|
||||
}
|
||||
}
|
||||
|
||||
func testLockPreventionDoesNotPreventReadInWALMode() throws {
|
||||
let dbQueue = try makeDatabaseQueue(journalMode: "wal")
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE t(a);")
|
||||
dbQueue.startPreventingLock()
|
||||
|
||||
try XCTAssertEqual(Int.fetchOne(db, sql: "SELECT COUNT(*) FROM t"), 0)
|
||||
|
||||
try db.inTransaction(.deferred) {
|
||||
try XCTAssertEqual(Int.fetchOne(db, sql: "SELECT COUNT(*) FROM t"), 0)
|
||||
return .commit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - INSERT
|
||||
|
||||
func testLockPreventionPreventsWriteInDeleteJournalMode() throws {
|
||||
let dbQueue = try makeDatabaseQueue(journalMode: "delete")
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE t(a);")
|
||||
dbQueue.startPreventingLock()
|
||||
do {
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ABORT)
|
||||
XCTAssertEqual(error.message, "Can't acquire lock")
|
||||
XCTAssertEqual(error.sql, "INSERT INTO t DEFAULT VALUES")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testLockPreventionPreventsWriteInWALMode() throws {
|
||||
let dbQueue = try makeDatabaseQueue(journalMode: "wal")
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE t(a);")
|
||||
dbQueue.startPreventingLock()
|
||||
|
||||
do {
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ABORT)
|
||||
XCTAssertEqual(error.message, "Can't acquire lock")
|
||||
XCTAssertEqual(error.sql, "INSERT INTO t DEFAULT VALUES")
|
||||
}
|
||||
|
||||
do {
|
||||
try db.inTransaction(.deferred) {
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
return .commit
|
||||
}
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ABORT)
|
||||
XCTAssertEqual(error.message, "Can't acquire lock")
|
||||
XCTAssertEqual(error.sql, "INSERT INTO t DEFAULT VALUES")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Automatic ROLLBACK
|
||||
|
||||
func testLockPreventionRollbacksOnPreventedWrite() throws {
|
||||
func test(_ dbQueue: DatabaseQueue) throws {
|
||||
do {
|
||||
try dbQueue.write { db in
|
||||
try db.execute(sql: "CREATE TABLE t(a);")
|
||||
XCTAssertTrue(db.isInsideTransaction)
|
||||
dbQueue.startPreventingLock()
|
||||
do {
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ABORT)
|
||||
XCTAssertEqual(error.message, "Can't acquire lock")
|
||||
XCTAssertEqual(error.sql, "INSERT INTO t DEFAULT VALUES")
|
||||
}
|
||||
// Aborded transaction
|
||||
XCTAssertFalse(db.isInsideTransaction)
|
||||
}
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ABORT)
|
||||
XCTAssertEqual(error.message, "Transaction was aborted")
|
||||
}
|
||||
}
|
||||
try test(makeDatabaseQueue(journalMode: "delete"))
|
||||
try test(makeDatabaseQueue(journalMode: "wal"))
|
||||
}
|
||||
|
||||
// MARK: - Concurrent Lock Prevention
|
||||
|
||||
func testLockPreventionAbortsDatabaseQueueAccess() throws {
|
||||
func test(_ dbQueue: DatabaseQueue) throws {
|
||||
let semaphore1 = DispatchSemaphore(value: 0)
|
||||
let semaphore2 = DispatchSemaphore(value: 0)
|
||||
dbQueue.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
|
||||
let block1 = {
|
||||
do {
|
||||
_ = try dbQueue.inDatabase {
|
||||
try Row.fetchAll($0, sql: "SELECT wait()")
|
||||
}
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_INTERRUPT)
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
}
|
||||
let block2 = {
|
||||
semaphore1.wait()
|
||||
dbQueue.startPreventingLock()
|
||||
semaphore2.signal()
|
||||
}
|
||||
let blocks = [block1, block2]
|
||||
DispatchQueue.concurrentPerform(iterations: blocks.count) { index in
|
||||
blocks[index]()
|
||||
}
|
||||
}
|
||||
try test(makeDatabaseQueue(journalMode: "delete"))
|
||||
try test(makeDatabaseQueue(journalMode: "wal"))
|
||||
}
|
||||
|
||||
func testLockPreventionDoesNotPreventFurtherReadInWALMode() throws {
|
||||
let dbQueue = try makeDatabaseQueue(journalMode: "wal")
|
||||
try dbQueue.write { db in
|
||||
try db.execute(sql: "CREATE TABLE t(a);")
|
||||
}
|
||||
|
||||
let semaphore1 = DispatchSemaphore(value: 0)
|
||||
let semaphore2 = DispatchSemaphore(value: 0)
|
||||
|
||||
dbQueue.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
let block1 = {
|
||||
do {
|
||||
try dbQueue.inTransaction { db in
|
||||
do {
|
||||
_ = try Row.fetchAll(db, sql: "SELECT wait()")
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_INTERRUPT)
|
||||
}
|
||||
XCTAssertTrue(db.isInsideTransaction)
|
||||
try XCTAssertEqual(Int.fetchOne(db, sql: "SELECT COUNT(*) FROM t"), 0)
|
||||
return .commit
|
||||
}
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
}
|
||||
let block2 = {
|
||||
semaphore1.wait()
|
||||
dbQueue.startPreventingLock()
|
||||
semaphore2.signal()
|
||||
}
|
||||
let blocks = [block1, block2]
|
||||
DispatchQueue.concurrentPerform(iterations: blocks.count) { index in
|
||||
blocks[index]()
|
||||
}
|
||||
}
|
||||
|
||||
func testWriteTransactionAbortedDuringStatementExecution() throws {
|
||||
func test(_ dbQueue: DatabaseQueue) throws {
|
||||
try dbQueue.write { db in
|
||||
try db.execute(sql: "CREATE TABLE t(a);")
|
||||
}
|
||||
|
||||
let semaphore1 = DispatchSemaphore(value: 0)
|
||||
let semaphore2 = DispatchSemaphore(value: 0)
|
||||
|
||||
dbQueue.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
let block1 = {
|
||||
do {
|
||||
try dbQueue.write { db in
|
||||
try db.execute(sql: "INSERT INTO t SELECT wait()")
|
||||
}
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
// Transactions throw the first uncatched error: SQLITE_INTERRUPT
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_INTERRUPT)
|
||||
XCTAssertEqual(error.message, "interrupted")
|
||||
XCTAssertEqual(error.sql, "INSERT INTO t SELECT wait()")
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
}
|
||||
let block2 = {
|
||||
semaphore1.wait()
|
||||
dbQueue.startPreventingLock()
|
||||
semaphore2.signal()
|
||||
}
|
||||
let blocks = [block1, block2]
|
||||
DispatchQueue.concurrentPerform(iterations: blocks.count) { index in
|
||||
blocks[index]()
|
||||
}
|
||||
}
|
||||
try test(makeDatabaseQueue(journalMode: "delete"))
|
||||
try test(makeDatabaseQueue(journalMode: "wal"))
|
||||
}
|
||||
|
||||
func testWriteTransactionAbortedDuringStatementExecutionPreventsFurtherDatabaseAccess() throws {
|
||||
func test(journalMode: String) throws {
|
||||
let dbQueue = try makeDatabaseQueue(journalMode: journalMode)
|
||||
try dbQueue.write { db in
|
||||
try db.execute(sql: "CREATE TABLE t(a);")
|
||||
}
|
||||
|
||||
let semaphore1 = DispatchSemaphore(value: 0)
|
||||
let semaphore2 = DispatchSemaphore(value: 0)
|
||||
|
||||
dbQueue.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
let block1 = {
|
||||
do {
|
||||
try dbQueue.write { db in
|
||||
do {
|
||||
try db.execute(sql: "INSERT INTO t SELECT wait()")
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_INTERRUPT)
|
||||
}
|
||||
|
||||
XCTAssertFalse(db.isInsideTransaction)
|
||||
|
||||
do {
|
||||
_ = try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM t")
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ABORT)
|
||||
switch journalMode {
|
||||
case "delete":
|
||||
XCTAssertEqual(error.message, "Can't acquire lock")
|
||||
case "wal":
|
||||
XCTAssertEqual(error.message, "Transaction was aborted")
|
||||
default:
|
||||
XCTFail()
|
||||
}
|
||||
XCTAssertEqual(error.sql, "SELECT COUNT(*) FROM t")
|
||||
}
|
||||
}
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
// SQLITE_INTERRUPT has been caught. So we get SQLITE_ABORT
|
||||
// from the last commit.
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ABORT)
|
||||
XCTAssertEqual(error.message, "Transaction was aborted")
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
}
|
||||
let block2 = {
|
||||
semaphore1.wait()
|
||||
dbQueue.startPreventingLock()
|
||||
semaphore2.signal()
|
||||
}
|
||||
let blocks = [block1, block2]
|
||||
DispatchQueue.concurrentPerform(iterations: blocks.count) { index in
|
||||
blocks[index]()
|
||||
}
|
||||
}
|
||||
try test(journalMode: "delete")
|
||||
try test(journalMode: "wal")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user