diff --git a/sandbox/src/commonMain/kotlin/main.kt b/sandbox/src/commonMain/kotlin/main.kt index 4237d38..e22cd73 100644 --- a/sandbox/src/commonMain/kotlin/main.kt +++ b/sandbox/src/commonMain/kotlin/main.kt @@ -5,15 +5,10 @@ val someStmt = PreparedStatementsCompilationPhantomCache("SELECT * FROM sqlite_m fun main() { for (i in 0..1000) { - DriverManager.getConnection("jdbc:sqlite::memory:").use { connection -> - someStmt.preparedStatementForConnection(connection).use { ps -> - ps.executeQuery().use { rs -> - while (rs.next()) { - println(rs.getString(0)) - } - } - } - } + val connection = DriverManager.getConnection("jdbc:sqlite::memory:") + val ps = someStmt.preparedStatementForConnection(connection) + val rs = ps.executeQuery(); + connection.close() System.gc() } } \ No newline at end of file diff --git a/src/jvmMain/java/ru/langrafhomyak/db/jdbc_resources_manager/_PhantomCacheCleanup.java b/src/jvmMain/java/ru/langrafhomyak/db/jdbc_resources_manager/_PhantomCacheCleanup.java index 184b6ad..27be433 100644 --- a/src/jvmMain/java/ru/langrafhomyak/db/jdbc_resources_manager/_PhantomCacheCleanup.java +++ b/src/jvmMain/java/ru/langrafhomyak/db/jdbc_resources_manager/_PhantomCacheCleanup.java @@ -26,7 +26,8 @@ class _PhantomCacheCleanup { @NotNull private static Thread _createThread() { - if (_PhantomCacheCleanup._isVirtualThreadsSupported() && !System.getProperty(_PhantomCacheCleanup._virtualThreadProperty).equalsIgnoreCase("false")) { + final String useVirtualSetting = System.getProperty(_PhantomCacheCleanup._virtualThreadProperty); + if (_PhantomCacheCleanup._isVirtualThreadsSupported() && (useVirtualSetting != null && !useVirtualSetting.equalsIgnoreCase("false"))) { return _PhantomCacheCleanup._startVirtualThread(); } else { return _PhantomCacheCleanup._startVirtualThread(); @@ -58,7 +59,7 @@ class _PhantomCacheCleanup { @NotNull private static Thread _startVirtualThread() { - final var b = Thread.ofVirtual(); + final Thread.Builder.OfVirtual b = Thread.ofVirtual(); b.name(_PhantomCacheCleanup._threadName); b.inheritInheritableThreadLocals(false); return b.start(new CleanupRoutine()); @@ -72,7 +73,10 @@ class _PhantomCacheCleanup { // (Thread.currentThread() as? InnocuousThread)?.eraseThreadLocals() try { - ((CleanablePhantomReference) _PhantomCacheCleanup._referenceQueue.remove(60 * 60)).cleanup(); + final CleanablePhantomReference cleanableRef = ((CleanablePhantomReference) _PhantomCacheCleanup._referenceQueue.remove(60 * 60)); + if (cleanableRef == null) + continue; + cleanableRef.cleanup(); } /* catch (_: LinkageError) { return } */ catch (InterruptedException e) {