Skip to content

Commit

Permalink
Move the logic into loadTimeZoneMappings.
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroyuki-sato committed Aug 18, 2024
1 parent 164272d commit 41163b9
Showing 1 changed file with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,8 @@ protected MySQLInputConnection newConnection(PluginTask task) throws SQLExceptio
props.putAll(t.getOptions());
logConnectionProperties(url, props);

// 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
}
// load timezone mappings
loadTimeZoneMappingsIfNeeded();

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

private void loadTimeZoneMappings(Class<?> timeUtilClass)
private void loadTimeZoneMappingsIfNeeded()
{
// Here initializes com.mysql.jdbc.TimeUtil.timeZoneMappings static field by calling
// static timeZoneMappings method using reflection.
Expand All @@ -163,9 +156,13 @@ private void loadTimeZoneMappings(Class<?> timeUtilClass)
// from the classloader. It seems like a bug of JDBC Driver where it should use the class loader
// that loaded com.mysql.jdbc.TimeUtil class rather than system class loader to read the
// property file because the file should be in the same classpath with the class.
// Here implements a workaround as as workaround.
// Here implements a workaround as a workaround.
//
// It's not 100% sure, but this workaround is necessary for Connector/J 5.x (com.mysql.jdbc.TimeUtil)
// only.
Field f = null;
try {
Class<?> timeUtilClass = Class.forName("com.mysql.jdbc.TimeUtil");
f = timeUtilClass.getDeclaredField("timeZoneMappings");
f.setAccessible(true);

Expand All @@ -178,6 +175,10 @@ private void loadTimeZoneMappings(Class<?> timeUtilClass)
f.set(null, timeZoneMappings);
}
}
catch (ClassNotFoundException e) {
// It appears that the user uses the Connector/J 8.x driver.
// Do nothing;
}
catch (IllegalAccessException | NoSuchFieldException | IOException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 41163b9

Please sign in to comment.