Skip to content

Commit 486d986

Browse files
author
diego Dupin
committed
[misc] failover improvement. some specific commands not in transaction are considered to be replayed in case of failover, like PING, PREPARE, ROLLBACK, ...
1 parent 8abced3 commit 486d986

File tree

14 files changed

+185
-86
lines changed

14 files changed

+185
-86
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
<configuration>
102102
<showWarnings>true</showWarnings>
103103
<compilerArgs>
104-
<arg>-Xlint:all,-options,-path,-processing,-requires-transitive-automatic,-requires-automatic</arg>
104+
<arg>-Xlint:all,-options,-path,-processing</arg>
105105
</compilerArgs>
106106
<source>1.8</source>
107107
<target>1.8</target>

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ private void executeInternal() throws SQLException {
7676
maxRows,
7777
resultSetConcurrency,
7878
resultSetType,
79-
closeOnCompletion);
79+
closeOnCompletion,
80+
false);
8081
} finally {
8182
lock.unlock();
8283
}
@@ -120,7 +121,8 @@ private void executeBatchBulk() throws SQLException {
120121
maxRows,
121122
ResultSet.CONCUR_READ_ONLY,
122123
ResultSet.TYPE_FORWARD_ONLY,
123-
closeOnCompletion);
124+
closeOnCompletion,
125+
false);
124126
// in case of failover, prepare is done in failover, skipping prepare result
125127
if (res.get(0) instanceof PrepareResultPacket) {
126128
results = res.subList(1, res.size());
@@ -137,7 +139,8 @@ private void executeBatchBulk() throws SQLException {
137139
maxRows,
138140
resultSetConcurrency,
139141
resultSetType,
140-
closeOnCompletion);
142+
closeOnCompletion,
143+
false);
141144
}
142145
} catch (SQLException bue) {
143146
results = null;
@@ -166,7 +169,8 @@ private void executeBatchPipeline() throws SQLException {
166169
maxRows,
167170
ResultSet.CONCUR_READ_ONLY,
168171
ResultSet.TYPE_FORWARD_ONLY,
169-
closeOnCompletion);
172+
closeOnCompletion,
173+
false);
170174
} catch (SQLException bue) {
171175
results = null;
172176
throw bue;
@@ -192,7 +196,8 @@ private void executeBatchStd() throws SQLException {
192196
maxRows,
193197
ResultSet.CONCUR_READ_ONLY,
194198
ResultSet.TYPE_FORWARD_ONLY,
195-
closeOnCompletion));
199+
closeOnCompletion,
200+
false));
196201
}
197202
} catch (SQLException bue) {
198203
BatchUpdateException exception =
@@ -383,7 +388,8 @@ public void setLargeMaxRows(long max) throws SQLException {
383388
public ResultSetMetaData getMetaData() throws SQLException {
384389

385390
// send COM_STMT_PREPARE
386-
if (prepareResult == null) con.getClient().execute(new PreparePacket(escapeTimeout(sql)), this);
391+
if (prepareResult == null)
392+
con.getClient().execute(new PreparePacket(escapeTimeout(sql)), this, true);
387393
return new org.mariadb.jdbc.client.result.ResultSetMetaData(
388394
exceptionFactory(), prepareResult.getColumns(), con.getContext().getConf(), false);
389395
}
@@ -405,7 +411,7 @@ public java.sql.ParameterMetaData getParameterMetaData() throws SQLException {
405411
// send COM_STMT_PREPARE
406412
if (prepareResult == null) {
407413
try {
408-
con.getClient().execute(new PreparePacket(escapeTimeout(sql)), this);
414+
con.getClient().execute(new PreparePacket(escapeTimeout(sql)), this, true);
409415
} catch (SQLException e) {
410416
return new SimpleParameterMetaData(exceptionFactory(), parser.getParamCount());
411417
}

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void setPoolConnection(MariaDbPoolConnection poolConnection) {
7575
public void cancelCurrentQuery() throws SQLException {
7676
try (Client cli =
7777
new StandardClient(conf, client.getHostAddress(), new ReentrantLock(), true)) {
78-
cli.execute(new QueryPacket("KILL QUERY " + client.getContext().getThreadId()));
78+
cli.execute(new QueryPacket("KILL QUERY " + client.getContext().getThreadId()), false);
7979
}
8080
}
8181

@@ -161,7 +161,8 @@ public void setAutoCommit(boolean autoCommit) throws SQLException {
161161
lock.lock();
162162
try {
163163
getContext().addStateFlag(ConnectionState.STATE_AUTOCOMMIT);
164-
client.execute(new QueryPacket(((autoCommit) ? "set autocommit=1" : "set autocommit=0")));
164+
client.execute(
165+
new QueryPacket(((autoCommit) ? "set autocommit=1" : "set autocommit=0")), true);
165166
} finally {
166167
lock.unlock();
167168
}
@@ -172,7 +173,7 @@ public void commit() throws SQLException {
172173
lock.lock();
173174
try {
174175
if ((client.getContext().getServerStatus() & ServerStatus.IN_TRANSACTION) > 0) {
175-
client.execute(new QueryPacket("COMMIT"));
176+
client.execute(new QueryPacket("COMMIT"), false);
176177
}
177178
} finally {
178179
lock.unlock();
@@ -184,7 +185,7 @@ public void rollback() throws SQLException {
184185
lock.lock();
185186
try {
186187
if ((client.getContext().getServerStatus() & ServerStatus.IN_TRANSACTION) > 0) {
187-
client.execute(new QueryPacket("ROLLBACK"));
188+
client.execute(new QueryPacket("ROLLBACK"), true);
188189
}
189190
} finally {
190191
lock.unlock();
@@ -279,7 +280,7 @@ public void setCatalog(String catalog) throws SQLException {
279280
lock.lock();
280281
try {
281282
getContext().addStateFlag(ConnectionState.STATE_DATABASE);
282-
client.execute(new ChangeDbPacket(catalog));
283+
client.execute(new ChangeDbPacket(catalog), true);
283284
client.getContext().setDatabase(catalog);
284285
} finally {
285286
lock.unlock();
@@ -349,7 +350,7 @@ public void setTransactionIsolation(int level) throws SQLException {
349350
checkNotClosed();
350351
getContext().addStateFlag(ConnectionState.STATE_TRANSACTION_ISOLATION);
351352
client.getContext().setTransactionIsolationLevel(level);
352-
client.execute(new QueryPacket(query));
353+
client.execute(new QueryPacket(query), true);
353354
} finally {
354355
lock.unlock();
355356
}
@@ -485,14 +486,14 @@ public void setHoldability(int holdability) {
485486
@Override
486487
public Savepoint setSavepoint() throws SQLException {
487488
MariaDbSavepoint savepoint = new MariaDbSavepoint(savepointId.incrementAndGet());
488-
client.execute(new QueryPacket("SAVEPOINT `" + savepoint.rawValue() + "`"));
489+
client.execute(new QueryPacket("SAVEPOINT `" + savepoint.rawValue() + "`"), true);
489490
return savepoint;
490491
}
491492

492493
@Override
493494
public Savepoint setSavepoint(String name) throws SQLException {
494495
MariaDbSavepoint savepoint = new MariaDbSavepoint(name.replace("`", "``"));
495-
client.execute(new QueryPacket("SAVEPOINT `" + savepoint.rawValue() + "`"));
496+
client.execute(new QueryPacket("SAVEPOINT `" + savepoint.rawValue() + "`"), true);
496497
return savepoint;
497498
}
498499

@@ -507,7 +508,8 @@ public void rollback(java.sql.Savepoint savepoint) throws SQLException {
507508
new QueryPacket(
508509
"ROLLBACK TO SAVEPOINT `"
509510
+ ((Connection.MariaDbSavepoint) savepoint).rawValue()
510-
+ "`"));
511+
+ "`"),
512+
true);
511513
} else {
512514
throw exceptionFactory.create("Unknown savepoint type");
513515
}
@@ -528,7 +530,8 @@ public void releaseSavepoint(java.sql.Savepoint savepoint) throws SQLException {
528530
new QueryPacket(
529531
"RELEASE SAVEPOINT `"
530532
+ ((Connection.MariaDbSavepoint) savepoint).rawValue()
531-
+ "`"));
533+
+ "`"),
534+
true);
532535
} else {
533536
throw exceptionFactory.create("Unknown savepoint type");
534537
}
@@ -620,7 +623,7 @@ public boolean isValid(int timeout) throws SQLException {
620623
}
621624
lock.lock();
622625
try {
623-
client.execute(PingPacket.INSTANCE);
626+
client.execute(PingPacket.INSTANCE, true);
624627
return true;
625628
} catch (SQLException sqle) {
626629
if (poolConnection != null) {
@@ -809,12 +812,12 @@ && getContext().getVersion().getMinorVersion() == 2
809812
&& getContext().getVersion().versionGreaterOrEqual(10, 2, 22)));
810813

811814
if (useComReset) {
812-
client.execute(ResetPacket.INSTANCE);
815+
client.execute(ResetPacket.INSTANCE, true);
813816
}
814817

815818
// in transaction => rollback
816819
if ((client.getContext().getServerStatus() & ServerStatus.IN_TRANSACTION) > 0) {
817-
client.execute(new QueryPacket("ROLLBACK"));
820+
client.execute(new QueryPacket("ROLLBACK"), true);
818821
}
819822

820823
int stateFlag = getContext().getStateFlag();

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public ServerPreparedStatement(
5252
defaultFetchSize);
5353
prepareResult = con.getContext().getPrepareCache().get(sql, this);
5454
if (prepareResult == null && !PREPARABLE_STATEMENT_PATTERN.matcher(sql).find()) {
55-
con.getClient().execute(new PreparePacket(sql), this);
55+
con.getClient().execute(new PreparePacket(sql), this, true);
5656
}
5757
parameters = new ParameterList();
5858
}
@@ -101,7 +101,8 @@ private void executePipeline(String cmd) throws SQLException {
101101
maxRows,
102102
resultSetConcurrency,
103103
resultSetType,
104-
closeOnCompletion);
104+
closeOnCompletion,
105+
false);
105106
results = res.subList(1, res.size());
106107
} catch (SQLException ex) {
107108
results = null;
@@ -114,7 +115,7 @@ private void executeStandard(String cmd) throws SQLException {
114115
if (prepareResult == null) {
115116
prepareResult = con.getContext().getPrepareCache().get(cmd, this);
116117
if (prepareResult == null) {
117-
con.getClient().execute(new PreparePacket(cmd), this);
118+
con.getClient().execute(new PreparePacket(cmd), this, true);
118119
}
119120
}
120121

@@ -129,7 +130,8 @@ private void executeStandard(String cmd) throws SQLException {
129130
maxRows,
130131
resultSetConcurrency,
131132
resultSetType,
132-
closeOnCompletion);
133+
closeOnCompletion,
134+
false);
133135
}
134136

135137
private void executeInternalPreparedBatch() throws SQLException {
@@ -176,7 +178,8 @@ private void executeBatchBulk(String cmd) throws SQLException {
176178
maxRows,
177179
ResultSet.CONCUR_READ_ONLY,
178180
ResultSet.TYPE_FORWARD_ONLY,
179-
closeOnCompletion);
181+
closeOnCompletion,
182+
false);
180183

181184
// in case of failover, prepare is done in failover, skipping prepare result
182185
if (res.get(0) instanceof PrepareResultPacket) {
@@ -194,7 +197,8 @@ private void executeBatchBulk(String cmd) throws SQLException {
194197
maxRows,
195198
ResultSet.CONCUR_READ_ONLY,
196199
ResultSet.TYPE_FORWARD_ONLY,
197-
closeOnCompletion);
200+
closeOnCompletion,
201+
false);
198202
}
199203

200204
} catch (SQLException bue) {
@@ -248,7 +252,8 @@ private List<Completion> executeBunch(String cmd, int index, int maxCmd) throws
248252
maxRows,
249253
ResultSet.CONCUR_READ_ONLY,
250254
ResultSet.TYPE_FORWARD_ONLY,
251-
closeOnCompletion);
255+
closeOnCompletion,
256+
false);
252257
}
253258

254259
private List<Completion> executeBunchPrepare(String cmd, int index, int maxCmd)
@@ -268,7 +273,8 @@ private List<Completion> executeBunchPrepare(String cmd, int index, int maxCmd)
268273
maxRows,
269274
ResultSet.CONCUR_READ_ONLY,
270275
ResultSet.TYPE_FORWARD_ONLY,
271-
closeOnCompletion);
276+
closeOnCompletion,
277+
false);
272278
// in case of failover, prepare is done in failover, skipping prepare result
273279
if (res.get(0) instanceof PrepareResultPacket) {
274280
return res.subList(1, res.size());
@@ -293,12 +299,12 @@ private void executeBatchStandard(String cmd) throws SQLException {
293299
if (prepareResult == null) {
294300
prepareResult = con.getContext().getPrepareCache().get(cmd, this);
295301
if (prepareResult == null) {
296-
con.getClient().execute(new PreparePacket(cmd), this);
302+
con.getClient().execute(new PreparePacket(cmd), this, false);
297303
}
298304
}
299305
try {
300306
ExecutePacket execute = new ExecutePacket(prepareResult, batchParameter, cmd, this);
301-
tmpResults.addAll(con.getClient().execute(execute, this));
307+
tmpResults.addAll(con.getClient().execute(execute, this, false));
302308
} catch (SQLException e) {
303309
if (error == null) error = e;
304310
}
@@ -517,7 +523,7 @@ public ResultSetMetaData getMetaData() throws SQLException {
517523

518524
// send COM_STMT_PREPARE
519525
if (prepareResult == null) {
520-
con.getClient().execute(new PreparePacket(escapeTimeout(sql)), this);
526+
con.getClient().execute(new PreparePacket(escapeTimeout(sql)), this, true);
521527
}
522528

523529
return new org.mariadb.jdbc.client.result.ResultSetMetaData(
@@ -540,7 +546,7 @@ public ResultSetMetaData getMetaData() throws SQLException {
540546
public java.sql.ParameterMetaData getParameterMetaData() throws SQLException {
541547
// send COM_STMT_PREPARE
542548
if (prepareResult == null) {
543-
con.getClient().execute(new PreparePacket(escapeTimeout(sql)), this);
549+
con.getClient().execute(new PreparePacket(escapeTimeout(sql)), this, true);
544550
}
545551

546552
return new ParameterMetaData(exceptionFactory(), prepareResult.getParameters());

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,8 @@ private void executeInternal(String sql, int autoGeneratedKeys) throws SQLExcept
879879
maxRows,
880880
this.resultSetConcurrency,
881881
this.resultSetType,
882-
closeOnCompletion);
882+
closeOnCompletion,
883+
false);
883884
} finally {
884885
lock.unlock();
885886
}
@@ -1423,7 +1424,8 @@ public List<Completion> executeInternalBatchPipeline() throws SQLException {
14231424
0L,
14241425
ResultSet.CONCUR_READ_ONLY,
14251426
ResultSet.TYPE_FORWARD_ONLY,
1426-
closeOnCompletion);
1427+
closeOnCompletion,
1428+
false);
14271429
}
14281430

14291431
public List<Completion> executeInternalBatchStandard() throws SQLException {
@@ -1439,7 +1441,8 @@ public List<Completion> executeInternalBatchStandard() throws SQLException {
14391441
0L,
14401442
ResultSet.CONCUR_READ_ONLY,
14411443
ResultSet.TYPE_FORWARD_ONLY,
1442-
closeOnCompletion));
1444+
closeOnCompletion,
1445+
false));
14431446
}
14441447
return results;
14451448
} catch (SQLException sqle) {

src/main/java/org/mariadb/jdbc/client/Client.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
public interface Client extends AutoCloseable {
1616

17-
List<Completion> execute(ClientMessage message) throws SQLException;
17+
List<Completion> execute(ClientMessage message, boolean canRedo) throws SQLException;
1818

19-
List<Completion> execute(ClientMessage message, org.mariadb.jdbc.Statement stmt)
19+
List<Completion> execute(ClientMessage message, org.mariadb.jdbc.Statement stmt, boolean canRedo)
2020
throws SQLException;
2121

2222
List<Completion> execute(
@@ -26,7 +26,8 @@ List<Completion> execute(
2626
long maxRows,
2727
int resultSetConcurrency,
2828
int resultSetType,
29-
boolean closeOnCompletion)
29+
boolean closeOnCompletion,
30+
boolean canRedo)
3031
throws SQLException;
3132

3233
List<Completion> executePipeline(
@@ -36,7 +37,8 @@ List<Completion> executePipeline(
3637
long maxRows,
3738
int resultSetConcurrency,
3839
int resultSetType,
39-
boolean closeOnCompletion)
40+
boolean closeOnCompletion,
41+
boolean canRedo)
4042
throws SQLException;
4143

4244
void readStreamingResults(

0 commit comments

Comments
 (0)