Skip to content

Commit

Permalink
Call loadTimeZoneMappings only if the TimeUtil class is com.mysql.jdb…
Browse files Browse the repository at this point in the history
…c.TimeUtil.
  • Loading branch information
hiroyuki-sato committed Aug 18, 2024
1 parent c4ca1d2 commit 164272d
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,15 @@ protected MySQLInputConnection newConnection(PluginTask task) throws SQLExceptio
props.putAll(t.getOptions());
logConnectionProperties(url, props);

// load timezone mappings
loadTimeZoneMappings();
// loadTimeZoneMappings initializes com.mysql.jdbc.TimeUtil.timeZoneMappings static field by calling
// static timeZoneMappings method using reflection.
// It may need Connector/J 5.x (com.mysql.jdbc.TimeUtil) only.
try {
Class<?> timeUtilClass = Class.forName("com.mysql.jdbc.TimeUtil");
loadTimeZoneMappings(timeUtilClass);
} catch (ClassNotFoundException e) {
// do nothing
}

Connection con = DriverManager.getConnection(url, props);
try {
Expand All @@ -145,7 +152,7 @@ protected ColumnGetterFactory newColumnGetterFactory(final PageBuilder pageBuild
return new MySQLColumnGetterFactory(pageBuilder, dateTimeZone);
}

private void loadTimeZoneMappings()
private void loadTimeZoneMappings(Class<?> timeUtilClass)
{
// Here initializes com.mysql.jdbc.TimeUtil.timeZoneMappings static field by calling
// static timeZoneMappings method using reflection.
Expand All @@ -159,7 +166,6 @@ private void loadTimeZoneMappings()
// Here implements a workaround as as workaround.
Field f = null;
try {
Class<?> timeUtilClass = Class.forName("com.mysql.jdbc.TimeUtil");
f = timeUtilClass.getDeclaredField("timeZoneMappings");
f.setAccessible(true);

Expand All @@ -172,7 +178,7 @@ private void loadTimeZoneMappings()
f.set(null, timeZoneMappings);
}
}
catch (ClassNotFoundException | IllegalAccessException | NoSuchFieldException | IOException e) {
catch (IllegalAccessException | NoSuchFieldException | IOException e) {
throw new RuntimeException(e);
}
finally {
Expand Down

0 comments on commit 164272d

Please sign in to comment.