Skip to content

Commit cabde57

Browse files
committed
Fix #8
1 parent e7678c0 commit cabde57

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

SQLiter/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
kotlin.code.style=official
1818

1919
GROUP=co.touchlab
20-
VERSION_NAME=0.5.7
20+
VERSION_NAME=0.5.8
2121

2222
STATELY_VERSION=0.5.1
2323
KOTLIN_VERSION=1.3.10

SQLiter/src/nativeCommonMain/kotlin/co/touchlab/sqliter/NativeDatabaseConnection.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ class NativeDatabaseConnection(
7777
}
7878

7979
override fun actualClose(nativePointerArg: Long) {
80+
nativeClose(nativePointerArg) //Call this first, in case it fails
8081
dbManager.decrementConnectionCount()
81-
nativeClose(nativePointerArg)
8282
}
8383

8484
fun migrateIfNeeded(

SQLiter/src/nativeCommonMain/kotlin/co/touchlab/sqliter/NativePointer.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ abstract class NativePointer(nativePointerArg: Long) {
3333
return now
3434
}
3535

36+
/**
37+
* Attempt to run 'actualClose' first. That may fail, in which case we don't want to mark
38+
* the pointer as closed
39+
*/
3640
fun closeNativePointer() = pointerLock.withLock {
3741
val local = nativePointerActual.value
38-
nativePointerActual.value = 0
3942
actualClose(local)
43+
nativePointerActual.value = 0
4044
}
4145

4246
val pointerClosed: Boolean

SQLiter/src/nativeCommonTest/kotlin/co/touchlab/sqliter/NativeDatabaseConnectionTest.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,29 @@ class NativeDatabaseConnectionTest : BaseDatabaseTest(){
324324
assertFails { conn.withStatement(insertSql, goInsert) }
325325
}
326326

327+
@Test
328+
fun testFailedCloseRecall(){
329+
val manager = createDatabaseManager(
330+
DatabaseConfiguration(
331+
name = TEST_DB_NAME, version = 1,
332+
create = { db ->
333+
db.withStatement(TWO_COL) {
334+
execute()
335+
336+
}
337+
338+
}, busyTimeout = 15000
339+
)
340+
)
341+
342+
val conn = manager.createMultiThreadedConnection()
343+
val stmt = conn.createStatement("select * from test")
344+
assertFails { conn.close() }
345+
assertFalse(conn.closed)
346+
stmt.finalizeStatement()
347+
conn.close()
348+
}
349+
327350

328351
private fun threadWait(time: Int, manager: DatabaseManager, block: (DatabaseConnection) -> Unit): Boolean {
329352
return manager.withConnection {

0 commit comments

Comments
 (0)