diff --git a/GRDB/Core/Database+Statements.swift b/GRDB/Core/Database+Statements.swift index c82e223cc..53b0c0d13 100644 --- a/GRDB/Core/Database+Statements.swift +++ b/GRDB/Core/Database+Statements.swift @@ -222,13 +222,8 @@ extension Database { extension Database { func executeUpdateStatement(_ statement: UpdateStatement) throws { - if preventsExclusiveLock.value && statement.createsExclusiveLock { - try? rollback() - throw DatabaseError( - resultCode: .SQLITE_ABORT, - message: "Can't acquire exclusive lock", - sql: statement.sql, - arguments: statement.arguments) + if statement.createsExclusiveLock { + try assertExclusiveLockPrevention(from: statement) } // In aborted transaction, forbid all statements but statements that @@ -305,13 +300,8 @@ extension Database { @inline(__always) func selectStatementWillExecute(_ statement: SelectStatement) throws { - if preventsExclusiveLock.value && !statement.isReadonly { - try? rollback() - throw DatabaseError( - resultCode: .SQLITE_ABORT, - message: "Can't acquire exclusive lock", - sql: statement.sql, - arguments: statement.arguments) + if !statement.isReadonly { + try assertExclusiveLockPrevention(from: statement) } try assertNotInsideAbortedTransactionBlock(sql: statement.sql, arguments: statement.arguments) diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 1d956b021..6509a765c 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -707,15 +707,27 @@ 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. - // See Database.executeUpdateStatement(_:) and - // Database.selectStatementWillExecute(_:). + // database access which requires an exclusive lock, + // in preventExclusiveLock(from:). } } func stopPreventingExclusiveLock() { preventsExclusiveLock.value = false } + + func assertExclusiveLockPrevention(from statement: Statement) throws { + if preventsExclusiveLock.value { + // Attempt at releasing eventual exclusive lock. + // See Database.startPreventingExclusiveLock() + try? rollback() + throw DatabaseError( + resultCode: .SQLITE_ABORT, + message: "Can't acquire exclusive lock", + sql: statement.sql, + arguments: statement.arguments) + } + } } extension Database {