diff --git a/CHANGELOG.md b/CHANGELOG.md index 701bca42f..03c89f96d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -124,6 +124,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: ## Next Release +- **Documentation Update**: The [Sharing a Database](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasesharing) guide was updated with a new recommendation. When an SQLite database is shared between multiple processes, writers should always perform IMMEDIATE transactions in order to avoid the `SQLITE_BUSY` error that can occur when transactions overlap. The new recommendation fits in a single line of code: `configuration.defaultTransactionKind = .immediate`. - **New**: Associations that involve views instead of tables are supported, but they require an explicit `ForeignKey` in their definition. Now a clear diagnostic message is emitted, instead of a unhelpful "no such table" runtime error. ## 6.24.1 diff --git a/GRDB/Documentation.docc/DatabaseSharing.md b/GRDB/Documentation.docc/DatabaseSharing.md index 1f2c3928e..50b9569f4 100644 --- a/GRDB/Documentation.docc/DatabaseSharing.md +++ b/GRDB/Documentation.docc/DatabaseSharing.md @@ -152,11 +152,14 @@ If several processes want to write in the database, configure the database pool ```swift var configuration = Configuration() +configuration.defaultTransactionKind = .immediate configuration.busyMode = .timeout(/* a TimeInterval */) let dbPool = try DatabasePool(path: ..., configuration: configuration) ``` -With such a setup, you may still get `SQLITE_BUSY` errors from all write operations. They will occur if the database remains locked by another process for longer than the specified timeout. You can catch those errors: +Both the `defaultTransactionKind` and `busyMode` are important for preventing `SQLITE_BUSY`. The `immediate` transaction kind prevents write transactions from overlapping, and the busy timeout has write transactions wait, instead of throwing `SQLITE_BUSY`, whenever another process is writing. + +With such a setup, you will still get `SQLITE_BUSY` errors if the database remains locked by another process for longer than the specified timeout. You can catch those errors: ```swift do { diff --git a/Tests/GRDBTests/DatabaseQueueTests.swift b/Tests/GRDBTests/DatabaseQueueTests.swift index 4a1b5f333..7bd5adf2b 100644 --- a/Tests/GRDBTests/DatabaseQueueTests.swift +++ b/Tests/GRDBTests/DatabaseQueueTests.swift @@ -258,6 +258,140 @@ class DatabaseQueueTests: GRDBTestCase { try test(qos: .userInitiated) } + // MARK: - SQLITE_BUSY prevention + + // See + func test_busy_timeout_does_not_prevent_SQLITE_BUSY_when_write_lock_is_acquired_by_other_connection() throws { + var configuration = dbConfiguration! + configuration.journalMode = .wal + configuration.busyMode = .timeout(1) // Does not help in this case + + let dbQueue1 = try makeDatabaseQueue(filename: "test", configuration: configuration) + let dbQueue2 = try makeDatabaseQueue(filename: "test", configuration: configuration) + + // DB1 DB2 + // BEGIN DEFERRED TRANSACTION + // READ + let s1 = DispatchSemaphore(value: 0) + // BEGIN DEFERRED TRANSACTION + // WRITE + let s2 = DispatchSemaphore(value: 0) + // WRITE -> SQLITE_BUSY because DB2 is already holding the write lock. + let s3 = DispatchSemaphore(value: 0) + // COMMIT + + let block1 = { + try! dbQueue1.inDatabase { db in + try db.inTransaction(.deferred) { + try db.execute(sql: "SELECT * FROM sqlite_master") + s1.signal() + s2.wait() + do { + try db.execute(sql: "CREATE TABLE test1(a)") + XCTFail("Expected error") + } catch DatabaseError.SQLITE_BUSY { + // Test success + } + s3.signal() + return .commit + } + } + } + + let block2 = { + try! dbQueue2.inDatabase { db in + s1.wait() + try db.inTransaction(.deferred) { + try db.execute(sql: "CREATE TABLE test2(a)") + s2.signal() + s3.wait() + return .commit + } + } + } + + let blocks = [block1, block2] + DispatchQueue.concurrentPerform(iterations: blocks.count) { index in + blocks[index]() + } + } + + // See + func test_busy_timeout_does_not_prevent_SQLITE_BUSY_when_write_lock_was_acquired_by_other_connection() throws { + var configuration = dbConfiguration! + configuration.journalMode = .wal + configuration.busyMode = .timeout(1) // Does not help in this case + + let dbQueue1 = try makeDatabaseQueue(filename: "test", configuration: configuration) + let dbQueue2 = try makeDatabaseQueue(filename: "test", configuration: configuration) + + // DB1 DB2 + // BEGIN DEFERRED TRANSACTION + // READ + let s1 = DispatchSemaphore(value: 0) + // WRITE + let s2 = DispatchSemaphore(value: 0) + // WRITE -> SQLITE_BUSY because write lock can't be acquired, + // even though DB2 does no longer hold any lock. + + let block1 = { + try! dbQueue1.inDatabase { db in + try db.inTransaction(.deferred) { + try db.execute(sql: "SELECT * FROM sqlite_master") + s1.signal() + s2.wait() + do { + try db.execute(sql: "CREATE TABLE test1(a)") + XCTFail("Expected error") + } catch DatabaseError.SQLITE_BUSY { + // Test success + } + return .commit + } + } + } + + let block2 = { + try! dbQueue2.inDatabase { db in + s1.wait() + try db.execute(sql: "CREATE TABLE test2(a)") + s2.signal() + } + } + + let blocks = [block1, block2] + DispatchQueue.concurrentPerform(iterations: blocks.count) { index in + blocks[index]() + } + } + + // See + func test_busy_timeout_and_IMMEDIATE_transactions_do_prevent_SQLITE_BUSY() throws { + var configuration = dbConfiguration! + // Test fails when this line is commented + configuration.defaultTransactionKind = .immediate + // Test fails when this line is commented + configuration.busyMode = .timeout(10) + + let dbQueue = try makeDatabaseQueue(filename: "test") + try dbQueue.inDatabase { db in + try db.execute(sql: "PRAGMA journal_mode = wal") + try db.execute(sql: "CREATE TABLE test(a)") + } + + let parallelWritesCount = 50 + DispatchQueue.concurrentPerform(iterations: parallelWritesCount) { index in + let dbQueue = try! makeDatabaseQueue(filename: "test", configuration: configuration) + try! dbQueue.write { db in + _ = try Table("test").fetchCount(db) + try db.execute(sql: "INSERT INTO test VALUES (1)") + } + } + + let count = try dbQueue.read(Table("test").fetchCount) + XCTAssertEqual(count, parallelWritesCount) + } + // MARK: - Closing func testClose() throws {