From cb0498d1996f7373a46982a2807c9c8a46c0ff20 Mon Sep 17 00:00:00 2001 From: Jesse Chavez Date: Tue, 6 Aug 2024 12:26:45 +1000 Subject: [PATCH] Mysql, fix active? method, it was forcing to reconnect when it should not --- lib/arjdbc/abstract/database_statements.rb | 2 +- lib/arjdbc/mysql/adapter.rb | 2 +- src/java/arjdbc/mysql/MySQLRubyJdbcConnection.java | 11 +++++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/arjdbc/abstract/database_statements.rb b/lib/arjdbc/abstract/database_statements.rb index 7186f06fd..bdff3e4a4 100644 --- a/lib/arjdbc/abstract/database_statements.rb +++ b/lib/arjdbc/abstract/database_statements.rb @@ -104,7 +104,7 @@ def convert_legacy_binds_to_attributes(binds) end end - def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: false) + def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true) log(sql, name, async: async) do with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn| conn.execute(sql) diff --git a/lib/arjdbc/mysql/adapter.rb b/lib/arjdbc/mysql/adapter.rb index a753d434e..1bf6ce612 100644 --- a/lib/arjdbc/mysql/adapter.rb +++ b/lib/arjdbc/mysql/adapter.rb @@ -199,7 +199,7 @@ def _quote(value) #++ def active? - !(@raw_connection.nil? || @raw_connection.closed?) && @lock.synchronize { @raw_connection&.execute_query("/* ping */ SELECT 1") } || false + !(@raw_connection.nil? || @raw_connection.closed?) && @lock.synchronize { @raw_connection&.ping } || false end alias :reset! :reconnect! diff --git a/src/java/arjdbc/mysql/MySQLRubyJdbcConnection.java b/src/java/arjdbc/mysql/MySQLRubyJdbcConnection.java index cc304205f..140f722ee 100644 --- a/src/java/arjdbc/mysql/MySQLRubyJdbcConnection.java +++ b/src/java/arjdbc/mysql/MySQLRubyJdbcConnection.java @@ -119,6 +119,17 @@ protected DriverWrapper newDriverWrapper(final ThreadContext context, final Stri return driverWrapper; } + @JRubyMethod(name = "ping") + public RubyBoolean db_ping(final ThreadContext context) { + final Connection connection = getConnection(true); + if (connection == null) return context.fals; + + // NOTE: It seems only `connection.isValid(aliveTimeout)` is needed + // for JDBC 4.0 and up. https://jira.mariadb.org/browse/CONJ-51 + + return context.runtime.newBoolean(isConnectionValid(context, connection)); + } + private static transient Class MYSQL_CONNECTION; private static transient Boolean MYSQL_CONNECTION_FOUND;