diff --git a/GRDB Tests/Public/Core/Concurrency/ConcurrencyTests.swift b/GRDB Tests/Public/Core/Concurrency/ConcurrencyTests.swift index 705a4b08d..1d339e04c 100644 --- a/GRDB Tests/Public/Core/Concurrency/ConcurrencyTests.swift +++ b/GRDB Tests/Public/Core/Concurrency/ConcurrencyTests.swift @@ -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") } } } diff --git a/GRDB Tests/Public/Core/Database/DatabaseTests.swift b/GRDB Tests/Public/Core/Database/DatabaseTests.swift index a043da346..1f833dfee 100644 --- a/GRDB Tests/Public/Core/Database/DatabaseTests.swift +++ b/GRDB Tests/Public/Core/Database/DatabaseTests.swift @@ -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) diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 113c9015c..494ddb61d 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -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 } } diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index 6ed4d5f17..d159cc5f0 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -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) {