Skip to content

Commit 19748d7

Browse files
committed
[CONJ-983] avoid race condition provoking locking issue
1 parent 14e8b9f commit 19748d7

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

src/main/java/org/mariadb/jdbc/MariaDbFunctionStatement.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public MariaDbFunctionStatement clone(MariaDbConnection connection)
130130
*/
131131
@Override
132132
public int executeUpdate() throws SQLException {
133-
connection.lock.lock();
133+
lock.lock();
134134
try {
135135
super.execute();
136136
retrieveOutputResult();
@@ -139,7 +139,7 @@ public int executeUpdate() throws SQLException {
139139
}
140140
return getUpdateCount();
141141
} finally {
142-
connection.lock.unlock();
142+
lock.unlock();
143143
}
144144
}
145145

@@ -157,7 +157,7 @@ public void setParameter(final int parameterIndex, final ParameterHolder holder)
157157

158158
@Override
159159
public ResultSet executeQuery() throws SQLException {
160-
connection.lock.lock();
160+
lock.lock();
161161
try {
162162
super.execute();
163163
retrieveOutputResult();
@@ -166,19 +166,19 @@ public ResultSet executeQuery() throws SQLException {
166166
}
167167
return SelectResultSet.createEmptyResultSet();
168168
} finally {
169-
connection.lock.unlock();
169+
lock.unlock();
170170
}
171171
}
172172

173173
@Override
174174
public boolean execute() throws SQLException {
175-
connection.lock.lock();
175+
lock.lock();
176176
try {
177177
super.execute();
178178
retrieveOutputResult();
179179
return results != null && results.getResultSet() == null;
180180
} finally {
181-
connection.lock.unlock();
181+
lock.unlock();
182182
}
183183
}
184184
}

src/main/java/org/mariadb/jdbc/MariaDbProcedureStatement.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,14 @@ public void setParameter(final int parameterIndex, final ParameterHolder holder)
164164

165165
@Override
166166
public boolean execute() throws SQLException {
167-
connection.lock.lock();
167+
lock.lock();
168168
try {
169169
validAllParameters();
170170
super.executeInternal(fetchSize);
171171
retrieveOutputResult();
172172
return results != null && results.getResultSet() != null;
173173
} finally {
174-
connection.lock.unlock();
174+
lock.unlock();
175175
}
176176
}
177177

src/main/java/org/mariadb/jdbc/MariaDbStatement.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@
5656
import java.nio.charset.Charset;
5757
import java.sql.*;
5858
import java.util.*;
59-
import java.util.concurrent.ExecutionException;
60-
import java.util.concurrent.Future;
61-
import java.util.concurrent.ScheduledExecutorService;
62-
import java.util.concurrent.TimeUnit;
59+
import java.util.concurrent.*;
6360
import java.util.concurrent.locks.ReentrantLock;
6461
import java.util.regex.Matcher;
6562
import java.util.regex.Pattern;
@@ -180,14 +177,16 @@ protected void setTimerTask(boolean isBatch) {
180177
timerTaskFuture =
181178
timeoutScheduler.schedule(
182179
() -> {
183-
try {
184-
isTimedout = true;
185-
if (!isBatch) {
186-
protocol.cancelCurrentQuery();
180+
if (protocol != null) {
181+
try {
182+
isTimedout = true;
183+
if (!isBatch) {
184+
protocol.cancelCurrentQuery();
185+
}
186+
protocol.interrupt();
187+
} catch (Throwable e) {
188+
// eat
187189
}
188-
protocol.interrupt();
189-
} catch (Throwable e) {
190-
// eat
191190
}
192191
},
193192
queryTimeout,
@@ -222,13 +221,10 @@ private void stopTimeoutTask() {
222221
if (timerTaskFuture != null) {
223222
if (!timerTaskFuture.cancel(true)) {
224223
// could not cancel, task either started or already finished
225-
// we must now wait for task to finish to ensure state modifications are done
224+
// we must now wait for task to finish ensuring state modifications are done
226225
try {
227226
timerTaskFuture.get();
228-
} catch (InterruptedException e) {
229-
// reset interrupt status
230-
Thread.currentThread().interrupt();
231-
} catch (ExecutionException e) {
227+
} catch (InterruptedException | ExecutionException | CancellationException e) {
232228
// ignore error, likely due to interrupting during cancel
233229
}
234230
// we don't catch the exception if already canceled, that would indicate we tried
@@ -801,6 +797,14 @@ public void close() throws SQLException {
801797
return;
802798
}
803799
connection.pooledConnection.fireStatementClosed(this);
800+
801+
if (timeoutScheduler != null) {
802+
try {
803+
timeoutScheduler.shutdown();
804+
} catch (Throwable t) {
805+
// eat
806+
}
807+
}
804808
} finally {
805809
protocol = null;
806810
connection = null;

0 commit comments

Comments
 (0)