mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Improve robustness of transaction error handling
This commit is contained in:
1 parent
bc6d4fc759
commit
17c81871db
4 files changed
+162
-155
No files matched your search
@@ -22,167 +22,145 @@ class ConcurrencyTests: XCTestCase {
|
||||
}
|
||||
|
||||
func testDeferredTransactionConcurrency() {
|
||||
do {
|
||||
try dbQueue1.inDatabase { db in
|
||||
try db.execute("CREATE TABLE stuffs (id INTEGER PRIMARY KEY)")
|
||||
try! dbQueue1.inDatabase { db in
|
||||
try db.execute("CREATE TABLE stuffs (id INTEGER PRIMARY KEY)")
|
||||
}
|
||||
|
||||
var concurrencyError: DatabaseError? = nil
|
||||
|
||||
let queue = NSOperationQueue()
|
||||
queue.maxConcurrentOperationCount = 2
|
||||
queue.addOperation(NSBlockOperation {
|
||||
do {
|
||||
try self.dbQueue1.inTransaction(.Deferred) { db in
|
||||
sleep(1) // make sure other transaction is opened
|
||||
try db.execute("INSERT INTO stuffs (id) VALUES (NULL)")
|
||||
sleep(1) // let other transaction try to update database
|
||||
return .Commit
|
||||
}
|
||||
}
|
||||
|
||||
var concurrencyError: DatabaseError? = nil
|
||||
|
||||
let queue = NSOperationQueue()
|
||||
queue.maxConcurrentOperationCount = 2
|
||||
queue.addOperation(NSBlockOperation {
|
||||
do {
|
||||
try self.dbQueue1.inTransaction(.Deferred) { db in
|
||||
sleep(1) // make sure other queue has opened its transaction.
|
||||
try db.execute("INSERT INTO stuffs (id) VALUES (NULL)")
|
||||
return .Commit
|
||||
}
|
||||
}
|
||||
catch let error as DatabaseError {
|
||||
concurrencyError = error
|
||||
}
|
||||
catch {
|
||||
XCTFail("\(error)")
|
||||
}
|
||||
})
|
||||
catch let error as DatabaseError {
|
||||
concurrencyError = error
|
||||
}
|
||||
catch {
|
||||
XCTFail("\(error)")
|
||||
}
|
||||
})
|
||||
|
||||
queue.addOperation(NSBlockOperation {
|
||||
do {
|
||||
try self.dbQueue2.inTransaction(.Deferred) { db in
|
||||
sleep(1) // make sure other queue has opened its transaction.
|
||||
try db.execute("INSERT INTO stuffs (id) VALUES (NULL)")
|
||||
return .Commit
|
||||
}
|
||||
queue.addOperation(NSBlockOperation {
|
||||
do {
|
||||
try self.dbQueue2.inTransaction(.Deferred) { db in
|
||||
sleep(1) // make sure other transaction is opened
|
||||
try db.execute("INSERT INTO stuffs (id) VALUES (NULL)")
|
||||
sleep(1) // let other transaction try to update database
|
||||
return .Commit
|
||||
}
|
||||
catch let error as DatabaseError {
|
||||
concurrencyError = error
|
||||
}
|
||||
catch {
|
||||
XCTFail("\(error)")
|
||||
}
|
||||
})
|
||||
|
||||
queue.waitUntilAllOperationsAreFinished()
|
||||
|
||||
if let concurrencyError = concurrencyError {
|
||||
XCTAssertEqual(concurrencyError.code, Int(SQLITE_BUSY))
|
||||
XCTAssertEqual(concurrencyError.sql, "INSERT INTO stuffs (id) VALUES (NULL)")
|
||||
} else {
|
||||
XCTFail("Expected concurrency error")
|
||||
}
|
||||
} catch {
|
||||
XCTFail("\(error)")
|
||||
catch let error as DatabaseError {
|
||||
concurrencyError = error
|
||||
}
|
||||
catch {
|
||||
XCTFail("\(error)")
|
||||
}
|
||||
})
|
||||
|
||||
queue.waitUntilAllOperationsAreFinished()
|
||||
|
||||
if let concurrencyError = concurrencyError {
|
||||
XCTAssertEqual(concurrencyError.code, Int(SQLITE_BUSY))
|
||||
XCTAssertEqual(concurrencyError.sql, "INSERT INTO stuffs (id) VALUES (NULL)")
|
||||
} else {
|
||||
XCTFail("Expected concurrency error")
|
||||
}
|
||||
}
|
||||
|
||||
func testExclusiveTransactionConcurrency() {
|
||||
do {
|
||||
try dbQueue1.inDatabase { db in
|
||||
try db.execute("CREATE TABLE stuffs (id INTEGER PRIMARY KEY)")
|
||||
var concurrencyError: DatabaseError? = nil
|
||||
|
||||
let queue = NSOperationQueue()
|
||||
queue.maxConcurrentOperationCount = 2
|
||||
queue.addOperation(NSBlockOperation {
|
||||
do {
|
||||
try self.dbQueue1.inTransaction(.Exclusive) { db in
|
||||
sleep(1) // let other queue try to open transaction
|
||||
return .Commit
|
||||
}
|
||||
}
|
||||
|
||||
var concurrencyError: DatabaseError? = nil
|
||||
|
||||
let queue = NSOperationQueue()
|
||||
queue.maxConcurrentOperationCount = 2
|
||||
queue.addOperation(NSBlockOperation {
|
||||
do {
|
||||
try self.dbQueue1.inTransaction(.Exclusive) { db in
|
||||
sleep(1) // make sure other queue has opened its transaction.
|
||||
try db.execute("INSERT INTO stuffs (id) VALUES (NULL)")
|
||||
return .Commit
|
||||
}
|
||||
}
|
||||
catch let error as DatabaseError {
|
||||
concurrencyError = error
|
||||
}
|
||||
catch {
|
||||
XCTFail("\(error)")
|
||||
}
|
||||
})
|
||||
|
||||
queue.addOperation(NSBlockOperation {
|
||||
do {
|
||||
try self.dbQueue2.inTransaction(.Exclusive) { db in
|
||||
sleep(1) // make sure other queue has opened its transaction.
|
||||
try db.execute("INSERT INTO stuffs (id) VALUES (NULL)")
|
||||
return .Commit
|
||||
}
|
||||
}
|
||||
catch let error as DatabaseError {
|
||||
concurrencyError = error
|
||||
}
|
||||
catch {
|
||||
XCTFail("\(error)")
|
||||
}
|
||||
})
|
||||
|
||||
queue.waitUntilAllOperationsAreFinished()
|
||||
|
||||
if let concurrencyError = concurrencyError {
|
||||
XCTAssertEqual(concurrencyError.code, Int(SQLITE_BUSY))
|
||||
XCTAssertEqual(concurrencyError.sql, "BEGIN EXCLUSIVE TRANSACTION")
|
||||
} else {
|
||||
XCTFail("Expected concurrency error")
|
||||
catch let error as DatabaseError {
|
||||
concurrencyError = error
|
||||
}
|
||||
} catch {
|
||||
XCTFail("\(error)")
|
||||
catch {
|
||||
XCTFail("\(error)")
|
||||
}
|
||||
})
|
||||
|
||||
queue.addOperation(NSBlockOperation {
|
||||
do {
|
||||
try self.dbQueue2.inTransaction(.Exclusive) { db in
|
||||
sleep(1) // let other queue try to open transaction
|
||||
return .Commit
|
||||
}
|
||||
}
|
||||
catch let error as DatabaseError {
|
||||
concurrencyError = error
|
||||
}
|
||||
catch {
|
||||
XCTFail("\(error)")
|
||||
}
|
||||
})
|
||||
|
||||
queue.waitUntilAllOperationsAreFinished()
|
||||
|
||||
if let concurrencyError = concurrencyError {
|
||||
XCTAssertEqual(concurrencyError.code, Int(SQLITE_BUSY))
|
||||
XCTAssertEqual(concurrencyError.sql, "BEGIN EXCLUSIVE TRANSACTION")
|
||||
} else {
|
||||
XCTFail("Expected concurrency error")
|
||||
}
|
||||
}
|
||||
|
||||
func testImmediateTransactionConcurrency() {
|
||||
do {
|
||||
try dbQueue1.inDatabase { db in
|
||||
try db.execute("CREATE TABLE stuffs (id INTEGER PRIMARY KEY)")
|
||||
var concurrencyError: DatabaseError? = nil
|
||||
|
||||
let queue = NSOperationQueue()
|
||||
queue.maxConcurrentOperationCount = 2
|
||||
queue.addOperation(NSBlockOperation {
|
||||
do {
|
||||
try self.dbQueue1.inTransaction(.Immediate) { db in
|
||||
sleep(1) // let other queue try to open transaction
|
||||
return .Commit
|
||||
}
|
||||
}
|
||||
|
||||
var concurrencyError: DatabaseError? = nil
|
||||
|
||||
let queue = NSOperationQueue()
|
||||
queue.maxConcurrentOperationCount = 2
|
||||
queue.addOperation(NSBlockOperation {
|
||||
do {
|
||||
try self.dbQueue1.inTransaction(.Immediate) { db in
|
||||
sleep(1) // make sure other queue has opened its transaction.
|
||||
try db.execute("INSERT INTO stuffs (id) VALUES (NULL)")
|
||||
return .Commit
|
||||
}
|
||||
}
|
||||
catch let error as DatabaseError {
|
||||
concurrencyError = error
|
||||
}
|
||||
catch {
|
||||
XCTFail("\(error)")
|
||||
}
|
||||
})
|
||||
|
||||
queue.addOperation(NSBlockOperation {
|
||||
do {
|
||||
try self.dbQueue2.inTransaction(.Immediate) { db in
|
||||
sleep(1) // make sure other queue has opened its transaction.
|
||||
try db.execute("INSERT INTO stuffs (id) VALUES (NULL)")
|
||||
return .Commit
|
||||
}
|
||||
}
|
||||
catch let error as DatabaseError {
|
||||
concurrencyError = error
|
||||
}
|
||||
catch {
|
||||
XCTFail("\(error)")
|
||||
}
|
||||
})
|
||||
|
||||
queue.waitUntilAllOperationsAreFinished()
|
||||
|
||||
if let concurrencyError = concurrencyError {
|
||||
XCTAssertEqual(concurrencyError.code, Int(SQLITE_BUSY))
|
||||
XCTAssertEqual(concurrencyError.sql, "BEGIN IMMEDIATE TRANSACTION")
|
||||
} else {
|
||||
XCTFail("Expected concurrency error")
|
||||
catch let error as DatabaseError {
|
||||
concurrencyError = error
|
||||
}
|
||||
} catch {
|
||||
XCTFail("\(error)")
|
||||
catch {
|
||||
XCTFail("\(error)")
|
||||
}
|
||||
})
|
||||
|
||||
queue.addOperation(NSBlockOperation {
|
||||
do {
|
||||
try self.dbQueue2.inTransaction(.Immediate) { db in
|
||||
sleep(1) // let other queue try to open transaction
|
||||
return .Commit
|
||||
}
|
||||
}
|
||||
catch let error as DatabaseError {
|
||||
concurrencyError = error
|
||||
}
|
||||
catch {
|
||||
XCTFail("\(error)")
|
||||
}
|
||||
})
|
||||
|
||||
queue.waitUntilAllOperationsAreFinished()
|
||||
|
||||
if let concurrencyError = concurrencyError {
|
||||
XCTAssertEqual(concurrencyError.code, Int(SQLITE_BUSY))
|
||||
XCTAssertEqual(concurrencyError.sql, "BEGIN IMMEDIATE TRANSACTION")
|
||||
} else {
|
||||
XCTFail("Expected concurrency error")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -394,10 +394,12 @@ class DatabaseTests : GRDBTestCase {
|
||||
|
||||
func testInTransactionInsideInDatabaseIsReentrant() {
|
||||
var success = false
|
||||
dbQueue.inDatabase { db in
|
||||
self.dbQueue.inTransaction { db in
|
||||
success = true
|
||||
return .Commit
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try self.dbQueue.inTransaction { db in
|
||||
success = true
|
||||
return .Commit
|
||||
}
|
||||
}
|
||||
}
|
||||
XCTAssertTrue(success)
|
||||
|
||||
@@ -307,7 +307,7 @@ public final class Database {
|
||||
*/
|
||||
func inTransaction(type: TransactionType, block: () throws -> TransactionCompletion) throws {
|
||||
var completion: TransactionCompletion = .Rollback
|
||||
var dbError: ErrorType? = nil
|
||||
var blockError: ErrorType? = nil
|
||||
|
||||
try beginTransaction(type)
|
||||
|
||||
@@ -315,18 +315,45 @@ public final class Database {
|
||||
completion = try block()
|
||||
} catch {
|
||||
completion = .Rollback
|
||||
dbError = error
|
||||
blockError = error
|
||||
}
|
||||
|
||||
switch completion {
|
||||
case .Commit:
|
||||
try commit()
|
||||
case .Rollback:
|
||||
try rollback()
|
||||
// https://www.sqlite.org/lang_transaction.html#immediate
|
||||
//
|
||||
// > Response To Errors Within A Transaction
|
||||
//
|
||||
// > If certain kinds of errors occur within a transaction, the
|
||||
// > transaction may or may not be rolled back automatically. The
|
||||
// > errors that can cause an automatic rollback include:
|
||||
// >
|
||||
// > - SQLITE_FULL: database or disk full
|
||||
// > - SQLITE_IOERR: disk I/O error
|
||||
// > - SQLITE_BUSY: database in use by another process
|
||||
// > - SQLITE_NOMEM: out or memory
|
||||
// >
|
||||
// > [...] It is recommended that applications respond to the errors
|
||||
// > listed above by explicitly issuing a ROLLBACK command. If the
|
||||
// > transaction has already been rolled back automatically by the
|
||||
// > error response, then the ROLLBACK command will fail with an
|
||||
// > error, but no harm is caused by this.
|
||||
if let blockError = blockError as? DatabaseError {
|
||||
switch Int32(blockError.code) {
|
||||
case SQLITE_FULL, SQLITE_IOERR, SQLITE_BUSY, SQLITE_NOMEM:
|
||||
do { try rollback() } catch { }
|
||||
default:
|
||||
try rollback()
|
||||
}
|
||||
} else {
|
||||
try rollback()
|
||||
}
|
||||
}
|
||||
|
||||
if let dbError = dbError {
|
||||
throw dbError
|
||||
if let blockError = blockError {
|
||||
throw blockError
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -117,7 +117,7 @@ public final class DatabaseQueue {
|
||||
.Commit or .Rollback.
|
||||
- throws: The error thrown by the block.
|
||||
*/
|
||||
public func inTransaction(type: Database.TransactionType = .Exclusive, block: (db: Database) throws -> Database.TransactionCompletion) rethrows {
|
||||
public func inTransaction(type: Database.TransactionType = .Exclusive, block: (db: Database) throws -> Database.TransactionCompletion) throws {
|
||||
let database = self.database
|
||||
try inQueue {
|
||||
try database.inTransaction(type) {
|
||||
|
||||
Reference in new issue
Block a user