Skip to content

Commit ff1566b

Browse files
committed
Add asserts about expected ends of transaction
1 parent 52c53c1 commit ff1566b

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

GRDB/Core/DatabasePool.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,9 @@ extension DatabasePool: DatabaseReader {
361361
return try await readerPool.get { reader in
362362
try await reader.execute { db in
363363
defer {
364-
try? db.commit() // Ignore commit error
364+
// Ignore commit error, but make sure we leave the transaction
365+
try? db.commit()
366+
assert(!db.isInsideTransaction)
365367
}
366368
// The block isolation comes from the DEFERRED transaction.
367369
try db.beginTransaction(.deferred)
@@ -385,7 +387,9 @@ extension DatabasePool: DatabaseReader {
385387
// Second async jump because that's how `Pool.async` has to be used.
386388
reader.async { db in
387389
defer {
388-
try? db.commit() // Ignore commit error
390+
// Ignore commit error, but make sure we leave the transaction
391+
try? db.commit()
392+
assert(!db.isInsideTransaction)
389393
releaseReader(.reuse)
390394
}
391395
do {
@@ -547,7 +551,9 @@ extension DatabasePool: DatabaseReader {
547551
let (reader, releaseReader) = try readerPool.get()
548552
reader.async { db in
549553
defer {
550-
try? db.commit() // Ignore commit error
554+
// Ignore commit error, but make sure we leave the transaction
555+
try? db.commit()
556+
assert(!db.isInsideTransaction)
551557
releaseReader(.reuse)
552558
}
553559
do {

GRDB/Core/DatabaseQueue.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,9 @@ extension DatabaseQueue: DatabaseReader {
248248
) {
249249
writer.async { db in
250250
defer {
251-
// Ignore error because we can not notify it.
251+
// Ignore commit error (we can not notify it), but make sure we leave the transaction
252252
try? db.commit()
253+
assert(!db.isInsideTransaction)
253254
try? db.endReadOnly()
254255
}
255256

@@ -296,8 +297,9 @@ extension DatabaseQueue: DatabaseReader {
296297
GRDBPrecondition(!db.isInsideTransaction, "must not be called from inside a transaction.")
297298

298299
defer {
299-
// Ignore error because we can not notify it.
300+
// Ignore commit error (we can not notify it), but make sure we leave the transaction
300301
try? db.commit()
302+
assert(!db.isInsideTransaction)
301303
try? db.endReadOnly()
302304
}
303305

GRDB/Core/DatabaseSnapshot.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ public final class DatabaseSnapshot {
113113
deinit {
114114
// Leave snapshot isolation
115115
reader.reentrantSync { db in
116+
// Ignore commit error (we can not notify it), but make sure we leave the transaction
116117
try? db.commit()
118+
assert(!db.isInsideTransaction)
117119
}
118120
}
119121

0 commit comments

Comments
 (0)