Skip to content

Commit

Permalink
[misc] set connection to autocommit if not the case and not explicitl…
Browse files Browse the repository at this point in the history
…y set to false.
  • Loading branch information
rusher committed Nov 26, 2023
1 parent c74a8e4 commit 4acb834
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
11 changes: 11 additions & 0 deletions src/main/java/org/mariadb/jdbc/client/ServerVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
26 changes: 18 additions & 8 deletions src/main/java/org/mariadb/jdbc/client/impl/StandardClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/mariadb/jdbc/util/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 4acb834

Please sign in to comment.