Skip to content

Commit 4acb834

Browse files
committedNov 26, 2023
[misc] set connection to autocommit if not the case and not explicitly set to false.
1 parent c74a8e4 commit 4acb834

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed
 

‎src/main/java/org/mariadb/jdbc/client/ServerVersion.java

+11
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ public interface ServerVersion {
4949
*/
5050
boolean versionGreaterOrEqual(int major, int minor, int patch);
5151

52+
53+
/**
54+
* Utility method to check if database version is greater than parameters.
55+
*
56+
* @param major exact major version
57+
* @param minor exact minor version
58+
* @param patch minimum patch version
59+
* @return true if version is greater than parameters
60+
*/
61+
boolean versionFixedMajorMinorGreaterOrEqual(int major, int minor, int patch);
62+
5263
/**
5364
* Is server mariadb
5465
*

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

+18-8
Original file line numberDiff line numberDiff line change
@@ -406,15 +406,25 @@ private void postConnectionQueries() throws SQLException {
406406
* @return sql setting session command
407407
*/
408408
public String createSessionVariableQuery(String serverTz, Context context) {
409-
// In JDBC, connection must start in autocommit mode
410-
// [CONJ-269] we cannot rely on serverStatus & ServerStatus.AUTOCOMMIT before this command to
411-
// avoid this command.
412-
// if autocommit=0 is set on server configuration, DB always send Autocommit on serverStatus
413-
// flag
414-
// after setting autocommit, we can rely on serverStatus value
415409
List<String> sessionCommands = new ArrayList<>();
416-
if (conf.autocommit() != null) {
417-
sessionCommands.add("autocommit=" + (conf.autocommit() ? "1" : "0"));
410+
411+
// In JDBC, connection must start in autocommit mode
412+
boolean canRelyOnConnectionFlag =
413+
context.getVersion().isMariaDBServer()
414+
&& (context.getVersion().versionFixedMajorMinorGreaterOrEqual(10, 4, 33)
415+
|| context.getVersion().versionFixedMajorMinorGreaterOrEqual(10, 5, 24)
416+
|| context.getVersion().versionFixedMajorMinorGreaterOrEqual(10, 6, 17)
417+
|| context.getVersion().versionFixedMajorMinorGreaterOrEqual(10, 11, 7)
418+
|| context.getVersion().versionFixedMajorMinorGreaterOrEqual(11, 0, 5)
419+
|| context.getVersion().versionFixedMajorMinorGreaterOrEqual(11, 1, 4)
420+
|| context.getVersion().versionFixedMajorMinorGreaterOrEqual(11, 2, 3));
421+
if ((conf.autocommit() == null && (context.getServerStatus() & ServerStatus.AUTOCOMMIT) == 0)
422+
|| (conf.autocommit() != null && !canRelyOnConnectionFlag)
423+
|| (conf.autocommit() != null
424+
&& canRelyOnConnectionFlag
425+
&& ((context.getServerStatus() & ServerStatus.AUTOCOMMIT) > 0) != conf.autocommit())) {
426+
sessionCommands.add(
427+
"autocommit=" + ((conf.autocommit() == null || conf.autocommit()) ? "1" : "0"));
418428
}
419429

420430
// add configured session variable if configured

‎src/main/java/org/mariadb/jdbc/util/Version.java

+16
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ public String getQualifier() {
7676
return qualifier;
7777
}
7878

79+
/**
80+
* Utility method to check if database version is greater than parameters.
81+
*
82+
* @param major exact major version
83+
* @param minor exact minor version
84+
* @param patch patch version
85+
* @return true if version is greater than parameters
86+
*/
87+
public boolean versionFixedMajorMinorGreaterOrEqual(int major, int minor, int patch) {
88+
if (this.majorVersion == major && this.minorVersion == minor && this.patchVersion >= patch) {
89+
return true;
90+
}
91+
return false;
92+
}
93+
94+
7995
/**
8096
* Utility method to check if database version is greater than parameters.
8197
*

0 commit comments

Comments
 (0)