Skip to content

Commit 320d699

Browse files
author
diego Dupin
committed
[CONJ-913] Avoid executing additional command on connection
1 parent b295d6d commit 320d699

File tree

14 files changed

+98
-84
lines changed

14 files changed

+98
-84
lines changed

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ public class Configuration {
5959

6060
// various
6161
private String timezone = null;
62-
private boolean autocommit = true;
62+
private Boolean autocommit = null;
6363
private boolean useMysqlMetadata = false;
64-
private TransactionIsolation transactionIsolation = TransactionIsolation.REPEATABLE_READ;
64+
private TransactionIsolation transactionIsolation = null;
6565
private int defaultFetchSize = 0;
6666
private int maxQuerySizeToLog = 1024;
67+
private Integer maxAllowedPacket = null;
6768
private String geometryDefaultType = null;
6869
private String restrictedAuth = null;
6970

@@ -150,11 +151,12 @@ private Configuration(
150151
HaMode haMode,
151152
Properties nonMappedOptions,
152153
String timezone,
153-
boolean autocommit,
154+
Boolean autocommit,
154155
boolean useMysqlMetadata,
155156
TransactionIsolation transactionIsolation,
156157
int defaultFetchSize,
157158
int maxQuerySizeToLog,
159+
Integer maxAllowedPacket,
158160
String geometryDefaultType,
159161
String restrictedAuth,
160162
String socketFactory,
@@ -221,6 +223,7 @@ private Configuration(
221223
this.transactionIsolation = transactionIsolation;
222224
this.defaultFetchSize = defaultFetchSize;
223225
this.maxQuerySizeToLog = maxQuerySizeToLog;
226+
this.maxAllowedPacket = maxAllowedPacket;
224227
this.geometryDefaultType = geometryDefaultType;
225228
this.restrictedAuth = restrictedAuth;
226229
this.socketFactory = socketFactory;
@@ -322,6 +325,7 @@ private Configuration(
322325
Integer defaultFetchSize,
323326
String tlsSocketType,
324327
Integer maxQuerySizeToLog,
328+
Integer maxAllowedPacket,
325329
Integer retriesAllDown,
326330
String galeraAllowedState,
327331
Boolean pool,
@@ -399,6 +403,7 @@ private Configuration(
399403
if (defaultFetchSize != null) this.defaultFetchSize = defaultFetchSize;
400404
if (tlsSocketType != null) this.tlsSocketType = tlsSocketType;
401405
if (maxQuerySizeToLog != null) this.maxQuerySizeToLog = maxQuerySizeToLog;
406+
if (maxAllowedPacket != null) this.maxAllowedPacket = maxAllowedPacket;
402407
if (retriesAllDown != null) this.retriesAllDown = retriesAllDown;
403408
if (galeraAllowedState != null) this.galeraAllowedState = galeraAllowedState;
404409
if (pool != null) this.pool = pool;
@@ -683,6 +688,7 @@ public Configuration clone(String username, String password) {
683688
this.transactionIsolation,
684689
this.defaultFetchSize,
685690
this.maxQuerySizeToLog,
691+
this.maxAllowedPacket,
686692
this.geometryDefaultType,
687693
this.restrictedAuth,
688694
this.socketFactory,
@@ -904,7 +910,7 @@ public boolean useBulkStmts() {
904910
return useBulkStmts;
905911
}
906912

907-
public boolean autocommit() {
913+
public Boolean autocommit() {
908914
return autocommit;
909915
}
910916

@@ -940,6 +946,10 @@ public int maxQuerySizeToLog() {
940946
return maxQuerySizeToLog;
941947
}
942948

949+
public Integer maxAllowedPacket() {
950+
return maxAllowedPacket;
951+
}
952+
943953
public int retriesAllDown() {
944954
return retriesAllDown;
945955
}
@@ -1188,6 +1198,7 @@ public static final class Builder implements Cloneable {
11881198
private Boolean useMysqlMetadata;
11891199
private Integer defaultFetchSize;
11901200
private Integer maxQuerySizeToLog;
1201+
private Integer maxAllowedPacket;
11911202
private String geometryDefaultType;
11921203
private String restrictedAuth;
11931204
private String transactionIsolation;
@@ -1651,6 +1662,11 @@ public Builder maxQuerySizeToLog(Integer maxQuerySizeToLog) {
16511662
return this;
16521663
}
16531664

1665+
public Builder maxAllowedPacket(Integer maxAllowedPacket) {
1666+
this.maxAllowedPacket = maxAllowedPacket;
1667+
return this;
1668+
}
1669+
16541670
public Builder retriesAllDown(Integer retriesAllDown) {
16551671
this.retriesAllDown = retriesAllDown;
16561672
return this;
@@ -1777,6 +1793,7 @@ public Configuration build() throws SQLException {
17771793
this.defaultFetchSize,
17781794
this.tlsSocketType,
17791795
this.maxQuerySizeToLog,
1796+
this.maxAllowedPacket,
17801797
this.retriesAllDown,
17811798
this.galeraAllowedState,
17821799
this.pool,

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -727,10 +727,6 @@ public boolean isWrapperFor(Class<?> iface) {
727727
return iface.isInstance(this);
728728
}
729729

730-
public int getWaitTimeout() {
731-
return client.getWaitTimeout();
732-
}
733-
734730
public Client getClient() {
735731
return client;
736732
}
@@ -822,7 +818,7 @@ && getContext().getVersion().getMinorVersion() == 2
822818
setNetworkTimeout(null, conf.socketTimeout());
823819
}
824820
if ((stateFlag & ConnectionState.STATE_AUTOCOMMIT) != 0) {
825-
setAutoCommit(conf.autocommit());
821+
setAutoCommit(conf.autocommit() == null ? true : conf.autocommit());
826822
}
827823
if ((stateFlag & ConnectionState.STATE_DATABASE) != 0) {
828824
setCatalog(conf.database());
@@ -831,7 +827,10 @@ && getContext().getVersion().getMinorVersion() == 2
831827
setReadOnly(false); // default to master connection
832828
}
833829
if (!useComReset && (stateFlag & ConnectionState.STATE_TRANSACTION_ISOLATION) != 0) {
834-
setTransactionIsolation(conf.transactionIsolation().getLevel());
830+
setTransactionIsolation(
831+
conf.transactionIsolation() == null
832+
? java.sql.Connection.TRANSACTION_REPEATABLE_READ
833+
: conf.transactionIsolation().getLevel());
835834
}
836835
} catch (SQLException sqle) {
837836
throw exceptionFactory.create("error resetting connection");

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ void readStreamingResults(
5858

5959
void setReadOnly(boolean readOnly) throws SQLException;
6060

61-
int getWaitTimeout();
62-
6361
int getSocketTimeout();
6462

6563
void setSocketTimeout(int milliseconds) throws SQLException;

src/main/java/org/mariadb/jdbc/client/impl/MultiPrimaryClient.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -469,11 +469,6 @@ public void setSocketTimeout(int milliseconds) throws SQLException {
469469
}
470470
}
471471

472-
@Override
473-
public int getWaitTimeout() {
474-
return currentClient.getWaitTimeout();
475-
}
476-
477472
@Override
478473
public boolean isClosed() {
479474
return closed;

src/main/java/org/mariadb/jdbc/client/impl/StandardClient.java

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
import java.sql.ResultSet;
1414
import java.sql.SQLException;
1515
import java.sql.SQLNonTransientConnectionException;
16-
import java.sql.SQLPermission;
1716
import java.time.Instant;
1817
import java.time.ZoneId;
1918
import java.time.ZoneOffset;
2019
import java.time.zone.ZoneRulesException;
2120
import java.util.*;
2221
import java.util.concurrent.Executor;
2322
import java.util.concurrent.locks.ReentrantLock;
23+
import java.util.stream.Collectors;
2424
import javax.net.ssl.SSLSocket;
2525
import org.mariadb.jdbc.Configuration;
2626
import org.mariadb.jdbc.HostAddress;
@@ -68,7 +68,6 @@ public class StandardClient implements Client, AutoCloseable {
6868
private org.mariadb.jdbc.Statement streamStmt = null;
6969
private ClientMessage streamMsg = null;
7070
private int socketTimeout;
71-
private int waitTimeout;
7271
private final boolean disablePipeline;
7372
protected Context context;
7473

@@ -208,7 +207,9 @@ public StandardClient(
208207
}
209208

210209
private void assignStream(OutputStream out, InputStream in, Configuration conf, Long threadId) {
211-
this.writer = new PacketWriter(out, conf.maxQuerySizeToLog(), sequence, compressionSequence);
210+
this.writer =
211+
new PacketWriter(
212+
out, conf.maxQuerySizeToLog(), conf.maxAllowedPacket(), sequence, compressionSequence);
212213
this.writer.setServerThreadId(threadId, hostAddress);
213214

214215
this.reader = new PacketReader(in, conf, sequence);
@@ -281,10 +282,6 @@ private String handleTimezone() throws SQLException {
281282

282283
private void postConnectionQueries() throws SQLException {
283284
List<String> commands = new ArrayList<>();
284-
String serverTz = conf.timezone() != null ? handleTimezone() : null;
285-
286-
commands.add(createSessionVariableQuery(serverTz));
287-
commands.add("SELECT @@max_allowed_packet, @@wait_timeout");
288285

289286
List<String> galeraAllowedStates =
290287
conf.galeraAllowedState() == null
@@ -297,6 +294,10 @@ private void postConnectionQueries() throws SQLException {
297294
commands.add("show status like 'wsrep_local_state'");
298295
}
299296

297+
String serverTz = conf.timezone() != null ? handleTimezone() : null;
298+
String sessionVariableQuery = createSessionVariableQuery(serverTz);
299+
if (sessionVariableQuery != null) commands.add(sessionVariableQuery);
300+
300301
if (hostAddress != null
301302
&& !hostAddress.primary
302303
&& context.getVersion().versionGreaterOrEqual(5, 6, 5)) {
@@ -320,13 +321,6 @@ private void postConnectionQueries() throws SQLException {
320321
false,
321322
true);
322323

323-
// read max allowed packet
324-
Result result = (Result) res.get(1);
325-
result.next();
326-
327-
waitTimeout = Integer.parseInt(result.getString(2));
328-
writer.setMaxAllowedPacket(Integer.parseInt(result.getString(1)));
329-
330324
if (hostAddress != null
331325
&& Boolean.TRUE.equals(hostAddress.primary)
332326
&& !galeraAllowedStates.isEmpty()) {
@@ -336,6 +330,7 @@ private void postConnectionQueries() throws SQLException {
336330
throw exceptionFactory.create(
337331
String.format("fail to validate Galera state (State is %s)", rs.getString(2)));
338332
}
333+
res.remove(0);
339334
}
340335

341336
} catch (SQLException sqlException) {
@@ -358,19 +353,14 @@ public String createSessionVariableQuery(String serverTz) {
358353
// if autocommit=0 is set on server configuration, DB always send Autocommit on serverStatus
359354
// flag
360355
// after setting autocommit, we can rely on serverStatus value
361-
StringBuilder sb = new StringBuilder();
362-
sb.append("autocommit=")
363-
.append(conf.autocommit() ? "1" : "0")
364-
.append(", sql_mode = concat(@@sql_mode,',STRICT_TRANS_TABLES')");
365-
366-
// force schema tracking if available
367-
if ((context.getServerCapabilities() & Capabilities.CLIENT_SESSION_TRACK) != 0) {
368-
sb.append(", session_track_schema=1");
356+
List<String> sessionCommands = new ArrayList<>();
357+
if (conf.autocommit() != null) {
358+
sessionCommands.add("autocommit=" + (conf.autocommit() ? "1" : "0"));
369359
}
370360

371361
// add configured session variable if configured
372362
if (conf.sessionVariables() != null) {
373-
sb.append(",").append(Security.parseSessionVariables(conf.sessionVariables()));
363+
sessionCommands.add(Security.parseSessionVariables(conf.sessionVariables()));
374364
}
375365

376366
// force client timezone to connection to ensure result of now(), ...
@@ -391,25 +381,29 @@ public String createSessionVariableQuery(String serverTz) {
391381
if (mustSetTimezone) {
392382
if (clientZoneId.getRules().isFixedOffset()) {
393383
ZoneOffset zoneOffset = clientZoneId.getRules().getOffset(Instant.now());
394-
sb.append(",time_zone='").append(zoneOffset.getId()).append("'");
384+
sessionCommands.add("time_zone='" + zoneOffset.getId() + "'");
395385
} else {
396-
sb.append(",time_zone='").append(conf.timezone()).append("'");
386+
sessionCommands.add("time_zone='" + conf.timezone() + "'");
397387
}
398388
}
399389
}
400390

401-
sb.append(",");
402-
int major = context.getVersion().getMajorVersion();
403-
if (!context.getVersion().isMariaDBServer()
404-
&& ((major >= 8 && context.getVersion().versionGreaterOrEqual(8, 0, 3))
405-
|| (major < 8 && context.getVersion().versionGreaterOrEqual(5, 7, 20)))) {
406-
sb.append("transaction_isolation");
407-
} else {
408-
sb.append("tx_isolation");
391+
if (conf.transactionIsolation() != null) {
392+
int major = context.getVersion().getMajorVersion();
393+
if (!context.getVersion().isMariaDBServer()
394+
&& ((major >= 8 && context.getVersion().versionGreaterOrEqual(8, 0, 3))
395+
|| (major < 8 && context.getVersion().versionGreaterOrEqual(5, 7, 20)))) {
396+
sessionCommands.add(
397+
"transaction_isolation='" + conf.transactionIsolation().getValue() + "'");
398+
} else {
399+
sessionCommands.add("tx_isolation='" + conf.transactionIsolation().getValue() + "'");
400+
}
409401
}
410-
sb.append("='").append(conf.transactionIsolation().getValue()).append("'");
411402

412-
return "set " + sb;
403+
if (!sessionCommands.isEmpty()) {
404+
return "set " + sessionCommands.stream().collect(Collectors.joining(","));
405+
}
406+
return null;
413407
}
414408

415409
public void setReadOnly(boolean readOnly) throws SQLException {
@@ -807,11 +801,6 @@ private void closeSocket() {
807801
}
808802
}
809803

810-
@Override
811-
public int getWaitTimeout() {
812-
return waitTimeout;
813-
}
814-
815804
public boolean isClosed() {
816805
return closed;
817806
}

src/main/java/org/mariadb/jdbc/client/socket/Writer.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ public interface Writer {
112112

113113
long getCmdLength();
114114

115-
void setMaxAllowedPacket(int maxAllowedPacket);
116-
117115
void permitTrace(boolean permitTrace);
118116

119117
/**

src/main/java/org/mariadb/jdbc/client/socket/impl/PacketWriter.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class PacketWriter implements Writer {
3535
protected byte[] buf;
3636
protected int pos = 4;
3737
private int maxPacketLength = MAX_PACKET_LENGTH;
38-
private int maxAllowedPacket = Integer.MAX_VALUE;
38+
private int maxAllowedPacket;
3939
private long cmdLength;
4040
private boolean permitTrace = true;
4141
private String serverThreadLog = "";
@@ -51,13 +51,18 @@ public class PacketWriter implements Writer {
5151
* @param compressSequence compressed packet sequence
5252
*/
5353
public PacketWriter(
54-
OutputStream out, int maxQuerySizeToLog, MutableInt sequence, MutableInt compressSequence) {
54+
OutputStream out,
55+
int maxQuerySizeToLog,
56+
Integer maxAllowedPacket,
57+
MutableInt sequence,
58+
MutableInt compressSequence) {
5559
this.out = out;
5660
this.buf = new byte[SMALL_BUFFER_SIZE];
5761
this.maxQuerySizeToLog = maxQuerySizeToLog;
5862
this.cmdLength = 0;
5963
this.sequence = sequence;
6064
this.compressSequence = compressSequence;
65+
this.maxAllowedPacket = maxAllowedPacket == null ? Integer.MAX_VALUE : maxAllowedPacket;
6166
}
6267

6368
public int pos() {
@@ -699,11 +704,6 @@ public boolean throwMaxAllowedLength(int length) {
699704
return cmdLength + length >= maxAllowedPacket;
700705
}
701706

702-
public void setMaxAllowedPacket(int maxAllowedPacket) {
703-
this.maxAllowedPacket = maxAllowedPacket;
704-
maxPacketLength = Math.min(MAX_PACKET_LENGTH, maxAllowedPacket + 4);
705-
}
706-
707707
public void permitTrace(boolean permitTrace) {
708708
this.permitTrace = permitTrace;
709709
}

0 commit comments

Comments
 (0)