diff --git a/src/main/java/org/mariadb/jdbc/MariaDbFunctionStatement.java b/src/main/java/org/mariadb/jdbc/MariaDbFunctionStatement.java index e824b4e5d..452424800 100644 --- a/src/main/java/org/mariadb/jdbc/MariaDbFunctionStatement.java +++ b/src/main/java/org/mariadb/jdbc/MariaDbFunctionStatement.java @@ -130,7 +130,7 @@ public MariaDbFunctionStatement clone(MariaDbConnection connection) */ @Override public int executeUpdate() throws SQLException { - connection.lock.lock(); + lock.lock(); try { super.execute(); retrieveOutputResult(); @@ -139,7 +139,7 @@ public int executeUpdate() throws SQLException { } return getUpdateCount(); } finally { - connection.lock.unlock(); + lock.unlock(); } } @@ -157,7 +157,7 @@ public void setParameter(final int parameterIndex, final ParameterHolder holder) @Override public ResultSet executeQuery() throws SQLException { - connection.lock.lock(); + lock.lock(); try { super.execute(); retrieveOutputResult(); @@ -166,19 +166,19 @@ public ResultSet executeQuery() throws SQLException { } return SelectResultSet.createEmptyResultSet(); } finally { - connection.lock.unlock(); + lock.unlock(); } } @Override public boolean execute() throws SQLException { - connection.lock.lock(); + lock.lock(); try { super.execute(); retrieveOutputResult(); return results != null && results.getResultSet() == null; } finally { - connection.lock.unlock(); + lock.unlock(); } } } diff --git a/src/main/java/org/mariadb/jdbc/MariaDbProcedureStatement.java b/src/main/java/org/mariadb/jdbc/MariaDbProcedureStatement.java index 75e961225..194fa3113 100644 --- a/src/main/java/org/mariadb/jdbc/MariaDbProcedureStatement.java +++ b/src/main/java/org/mariadb/jdbc/MariaDbProcedureStatement.java @@ -164,14 +164,14 @@ public void setParameter(final int parameterIndex, final ParameterHolder holder) @Override public boolean execute() throws SQLException { - connection.lock.lock(); + lock.lock(); try { validAllParameters(); super.executeInternal(fetchSize); retrieveOutputResult(); return results != null && results.getResultSet() != null; } finally { - connection.lock.unlock(); + lock.unlock(); } } diff --git a/src/main/java/org/mariadb/jdbc/MariaDbStatement.java b/src/main/java/org/mariadb/jdbc/MariaDbStatement.java index 38716cbe5..1ee5fe9c6 100755 --- a/src/main/java/org/mariadb/jdbc/MariaDbStatement.java +++ b/src/main/java/org/mariadb/jdbc/MariaDbStatement.java @@ -56,10 +56,7 @@ import java.nio.charset.Charset; import java.sql.*; import java.util.*; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.*; import java.util.concurrent.locks.ReentrantLock; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -180,14 +177,16 @@ protected void setTimerTask(boolean isBatch) { timerTaskFuture = timeoutScheduler.schedule( () -> { - try { - isTimedout = true; - if (!isBatch) { - protocol.cancelCurrentQuery(); + if (protocol != null) { + try { + isTimedout = true; + if (!isBatch) { + protocol.cancelCurrentQuery(); + } + protocol.interrupt(); + } catch (Throwable e) { + // eat } - protocol.interrupt(); - } catch (Throwable e) { - // eat } }, queryTimeout, @@ -222,13 +221,10 @@ private void stopTimeoutTask() { if (timerTaskFuture != null) { if (!timerTaskFuture.cancel(true)) { // could not cancel, task either started or already finished - // we must now wait for task to finish to ensure state modifications are done + // we must now wait for task to finish ensuring state modifications are done try { timerTaskFuture.get(); - } catch (InterruptedException e) { - // reset interrupt status - Thread.currentThread().interrupt(); - } catch (ExecutionException e) { + } catch (InterruptedException | ExecutionException | CancellationException e) { // ignore error, likely due to interrupting during cancel } // we don't catch the exception if already canceled, that would indicate we tried @@ -801,6 +797,14 @@ public void close() throws SQLException { return; } connection.pooledConnection.fireStatementClosed(this); + + if (timeoutScheduler != null) { + try { + timeoutScheduler.shutdown(); + } catch (Throwable t) { + // eat + } + } } finally { protocol = null; connection = null;