diff --git a/src/main/java/org/mariadb/jdbc/client/ServerVersion.java b/src/main/java/org/mariadb/jdbc/client/ServerVersion.java index 303dbb928..eb492d414 100644 --- a/src/main/java/org/mariadb/jdbc/client/ServerVersion.java +++ b/src/main/java/org/mariadb/jdbc/client/ServerVersion.java @@ -49,6 +49,17 @@ public interface ServerVersion { */ boolean versionGreaterOrEqual(int major, int minor, int patch); + + /** + * Utility method to check if database version is greater than parameters. + * + * @param major exact major version + * @param minor exact minor version + * @param patch minimum patch version + * @return true if version is greater than parameters + */ + boolean versionFixedMajorMinorGreaterOrEqual(int major, int minor, int patch); + /** * Is server mariadb * diff --git a/src/main/java/org/mariadb/jdbc/client/impl/StandardClient.java b/src/main/java/org/mariadb/jdbc/client/impl/StandardClient.java index 9d3718c10..2776307b2 100644 --- a/src/main/java/org/mariadb/jdbc/client/impl/StandardClient.java +++ b/src/main/java/org/mariadb/jdbc/client/impl/StandardClient.java @@ -406,15 +406,25 @@ private void postConnectionQueries() throws SQLException { * @return sql setting session command */ public String createSessionVariableQuery(String serverTz, Context context) { - // In JDBC, connection must start in autocommit mode - // [CONJ-269] we cannot rely on serverStatus & ServerStatus.AUTOCOMMIT before this command to - // avoid this command. - // if autocommit=0 is set on server configuration, DB always send Autocommit on serverStatus - // flag - // after setting autocommit, we can rely on serverStatus value List sessionCommands = new ArrayList<>(); - if (conf.autocommit() != null) { - sessionCommands.add("autocommit=" + (conf.autocommit() ? "1" : "0")); + + // In JDBC, connection must start in autocommit mode + boolean canRelyOnConnectionFlag = + context.getVersion().isMariaDBServer() + && (context.getVersion().versionFixedMajorMinorGreaterOrEqual(10, 4, 33) + || context.getVersion().versionFixedMajorMinorGreaterOrEqual(10, 5, 24) + || context.getVersion().versionFixedMajorMinorGreaterOrEqual(10, 6, 17) + || context.getVersion().versionFixedMajorMinorGreaterOrEqual(10, 11, 7) + || context.getVersion().versionFixedMajorMinorGreaterOrEqual(11, 0, 5) + || context.getVersion().versionFixedMajorMinorGreaterOrEqual(11, 1, 4) + || context.getVersion().versionFixedMajorMinorGreaterOrEqual(11, 2, 3)); + if ((conf.autocommit() == null && (context.getServerStatus() & ServerStatus.AUTOCOMMIT) == 0) + || (conf.autocommit() != null && !canRelyOnConnectionFlag) + || (conf.autocommit() != null + && canRelyOnConnectionFlag + && ((context.getServerStatus() & ServerStatus.AUTOCOMMIT) > 0) != conf.autocommit())) { + sessionCommands.add( + "autocommit=" + ((conf.autocommit() == null || conf.autocommit()) ? "1" : "0")); } // add configured session variable if configured diff --git a/src/main/java/org/mariadb/jdbc/util/Version.java b/src/main/java/org/mariadb/jdbc/util/Version.java index a5ef4298e..6c6ba4051 100644 --- a/src/main/java/org/mariadb/jdbc/util/Version.java +++ b/src/main/java/org/mariadb/jdbc/util/Version.java @@ -76,6 +76,22 @@ public String getQualifier() { return qualifier; } + /** + * Utility method to check if database version is greater than parameters. + * + * @param major exact major version + * @param minor exact minor version + * @param patch patch version + * @return true if version is greater than parameters + */ + public boolean versionFixedMajorMinorGreaterOrEqual(int major, int minor, int patch) { + if (this.majorVersion == major && this.minorVersion == minor && this.patchVersion >= patch) { + return true; + } + return false; + } + + /** * Utility method to check if database version is greater than parameters. *