Skip to content

Commit

Permalink
Add support user-defined time zone for mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
hqbhoho committed Dec 26, 2024
1 parent 8957c92 commit 2054bf2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ public static Properties getConnectionProperties(MySqlConfig mySqlConfig)
connectionProperties.setProperty("characterEncoding", "utf8");
connectionProperties.setProperty("tinyInt1isBit", "false");
connectionProperties.setProperty("rewriteBatchedStatements", "true");

// connectionTimeZone = LOCAL means the JDBC driver uses the JVM zone as the session zone
// forceConnectionTimeZoneToSession = true means that the server side connection zone is changed to match local JVM zone
// https://dev.mysql.com/doc/connector-j/en/connector-j-time-instants.html (Solution 2b)
connectionProperties.setProperty("connectionTimeZone", "LOCAL");
connectionProperties.setProperty("forceConnectionTimeZoneToSession", "true");

if (mySqlConfig.isForceConnectionTimeZoneToSession()) {
// connectionTimeZone = LOCAL means the JDBC driver uses the JVM zone as the session zone
// forceConnectionTimeZoneToSession = true means that the server side connection zone is changed to match local JVM zone
// https://dev.mysql.com/doc/connector-j/en/connector-j-time-instants.html (Solution 2b)
connectionProperties.setProperty("connectionTimeZone", "LOCAL");
connectionProperties.setProperty("forceConnectionTimeZoneToSession", "true");
}
if (mySqlConfig.isAutoReconnect()) {
connectionProperties.setProperty("autoReconnect", String.valueOf(mySqlConfig.isAutoReconnect()));
connectionProperties.setProperty("maxReconnects", String.valueOf(mySqlConfig.getMaxReconnects()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class MySqlConfig
// Using `useInformationSchema=false` may provide more diagnostic information (see https://github.com/trinodb/trino/issues/1597)
private boolean driverUseInformationSchema = true;

private boolean forceConnectionTimeZoneToSession = true;

public boolean isAutoReconnect()
{
return autoReconnect;
Expand Down Expand Up @@ -80,4 +82,17 @@ public MySqlConfig setDriverUseInformationSchema(boolean driverUseInformationSch
this.driverUseInformationSchema = driverUseInformationSchema;
return this;
}

public boolean isForceConnectionTimeZoneToSession()
{
return forceConnectionTimeZoneToSession;
}

@Config("mysql.jdbc.force-connection-time-zone-to-session")
@ConfigDescription("Value of forceConnectionTimeZoneToSession MySQL JDBC driver connection property")
public MySqlConfig setForceConnectionTimeZoneToSession(boolean forceConnectionTimeZoneToSession)
{
this.forceConnectionTimeZoneToSession = forceConnectionTimeZoneToSession;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public void testDefaults()
.setAutoReconnect(true)
.setMaxReconnects(3)
.setConnectionTimeout(new Duration(10, TimeUnit.SECONDS))
.setDriverUseInformationSchema(true));
.setDriverUseInformationSchema(true)
.setForceConnectionTimeZoneToSession(true));
}

@Test
Expand All @@ -44,13 +45,15 @@ public void testExplicitPropertyMappings()
.put("mysql.max-reconnects", "4")
.put("mysql.connection-timeout", "4s")
.put("mysql.jdbc.use-information-schema", "false")
.put("mysql.jdbc.force-connection-time-zone-to-session", "false")
.buildOrThrow();

MySqlConfig expected = new MySqlConfig()
.setAutoReconnect(false)
.setMaxReconnects(4)
.setConnectionTimeout(new Duration(4, TimeUnit.SECONDS))
.setDriverUseInformationSchema(false);
.setDriverUseInformationSchema(false)
.setForceConnectionTimeZoneToSession(false);

assertFullMapping(properties, expected);
}
Expand Down

0 comments on commit 2054bf2

Please sign in to comment.