Skip to content

Commit

Permalink
[CONJ-983] avoid race condition provoking locking issue
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Jun 21, 2022
1 parent 14e8b9f commit 19748d7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
12 changes: 6 additions & 6 deletions src/main/java/org/mariadb/jdbc/MariaDbFunctionStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public MariaDbFunctionStatement clone(MariaDbConnection connection)
*/
@Override
public int executeUpdate() throws SQLException {
connection.lock.lock();
lock.lock();
try {
super.execute();
retrieveOutputResult();
Expand All @@ -139,7 +139,7 @@ public int executeUpdate() throws SQLException {
}
return getUpdateCount();
} finally {
connection.lock.unlock();
lock.unlock();
}
}

Expand All @@ -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();
Expand All @@ -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();
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/mariadb/jdbc/MariaDbProcedureStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
36 changes: 20 additions & 16 deletions src/main/java/org/mariadb/jdbc/MariaDbStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 19748d7

Please sign in to comment.