From dae15ef0e3d8adfb4751ab7e855ce239d36a015b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EC=9A=B0?= Date: Mon, 27 Jan 2025 19:15:02 +0900 Subject: [PATCH] Implement limbo close() --- bindings/java/rs_src/limbo_connection.rs | 11 ++++- .../tursodatabase/core/LimboConnection.java | 41 ++++++++----------- .../tursodatabase/jdbc4/JDBC4Connection.java | 2 +- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/bindings/java/rs_src/limbo_connection.rs b/bindings/java/rs_src/limbo_connection.rs index dd54e9087..048310ce5 100644 --- a/bindings/java/rs_src/limbo_connection.rs +++ b/bindings/java/rs_src/limbo_connection.rs @@ -12,7 +12,6 @@ use std::rc::Rc; use std::sync::Arc; #[derive(Clone)] -#[allow(dead_code)] pub struct LimboConnection { // Because java's LimboConnection is 1:1 mapped to limbo connection, we can use Rc pub(crate) conn: Rc, @@ -29,7 +28,6 @@ impl LimboConnection { Box::into_raw(Box::new(self)) as jlong } - #[allow(dead_code)] pub fn drop(ptr: jlong) { let _boxed = unsafe { Box::from_raw(ptr as *mut LimboConnection) }; } @@ -43,6 +41,15 @@ pub fn to_limbo_connection(ptr: jlong) -> Result<&'static mut LimboConnection> { } } +#[no_mangle] +pub extern "system" fn Java_org_github_tursodatabase_core_LimboConnection__1close<'local>( + _env: JNIEnv<'local>, + _obj: JObject<'local>, + connection_ptr: jlong, +) { + LimboConnection::drop(connection_ptr); +} + #[no_mangle] pub extern "system" fn Java_org_github_tursodatabase_core_LimboConnection_prepareUtf8<'local>( mut env: JNIEnv<'local>, diff --git a/bindings/java/src/main/java/org/github/tursodatabase/core/LimboConnection.java b/bindings/java/src/main/java/org/github/tursodatabase/core/LimboConnection.java index cd200f74f..5c101689e 100644 --- a/bindings/java/src/main/java/org/github/tursodatabase/core/LimboConnection.java +++ b/bindings/java/src/main/java/org/github/tursodatabase/core/LimboConnection.java @@ -16,6 +16,7 @@ public abstract class LimboConnection implements Connection { private final long connectionPtr; private final AbstractDB database; + private boolean closed; public LimboConnection(String url, String filePath) throws SQLException { this(url, filePath, new Properties()); @@ -28,24 +29,9 @@ public abstract class LimboConnection implements Connection { * @param filePath path to file */ public LimboConnection(String url, String filePath, Properties properties) throws SQLException { - AbstractDB db = null; - - try { - db = open(url, filePath, properties); - } catch (Throwable t) { - try { - if (db != null) { - db.close(); - } - } catch (Throwable t2) { - t.addSuppressed(t2); - } - - throw t; - } - - this.database = db; - this.connectionPtr = db.connect(); + this.database = open(url, filePath, properties); + this.connectionPtr = this.database.connect(); + this.closed = true; } private static AbstractDB open(String url, String filePath, Properties properties) @@ -59,13 +45,17 @@ public abstract class LimboConnection implements Connection { @Override public void close() throws SQLException { - if (isClosed()) return; - database.close(); + if (isClosed()) { + return; + } + this._close(this.connectionPtr); } + private native void _close(long connectionPtr); + @Override public boolean isClosed() throws SQLException { - return database.isClosed(); + return closed; } public AbstractDB getDatabase() { @@ -114,12 +104,15 @@ public abstract class LimboConnection implements Connection { */ protected void checkCursor(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { - if (resultSetType != ResultSet.TYPE_FORWARD_ONLY) + if (resultSetType != ResultSet.TYPE_FORWARD_ONLY) { throw new SQLException("SQLite only supports TYPE_FORWARD_ONLY cursors"); - if (resultSetConcurrency != ResultSet.CONCUR_READ_ONLY) + } + if (resultSetConcurrency != ResultSet.CONCUR_READ_ONLY) { throw new SQLException("SQLite only supports CONCUR_READ_ONLY cursors"); - if (resultSetHoldability != ResultSet.CLOSE_CURSORS_AT_COMMIT) + } + if (resultSetHoldability != ResultSet.CLOSE_CURSORS_AT_COMMIT) { throw new SQLException("SQLite only supports closing cursors at commit"); + } } public void setBusyTimeout(int busyTimeout) { diff --git a/bindings/java/src/main/java/org/github/tursodatabase/jdbc4/JDBC4Connection.java b/bindings/java/src/main/java/org/github/tursodatabase/jdbc4/JDBC4Connection.java index b17c6af36..2fa24b2ea 100644 --- a/bindings/java/src/main/java/org/github/tursodatabase/jdbc4/JDBC4Connection.java +++ b/bindings/java/src/main/java/org/github/tursodatabase/jdbc4/JDBC4Connection.java @@ -83,7 +83,7 @@ public class JDBC4Connection extends LimboConnection { @Override public void close() throws SQLException { - // TODO + super.close(); } @Override