Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initializes com.mysql.jdbc.TimeUtil.timeZoneMappings if needed (Connector/J 5.x) #259

Merged
merged 3 commits into from
Aug 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected MySQLInputConnection newConnection(PluginTask task) throws SQLExceptio
logConnectionProperties(url, props);

// load timezone mappings
loadTimeZoneMappings();
loadTimeZoneMappingsIfNeeded();

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

private void loadTimeZoneMappings()
private void loadTimeZoneMappingsIfNeeded()
{
// Here initializes com.mysql.jdbc.TimeUtil.timeZoneMappings static field by calling
// static timeZoneMappings method using reflection.
Expand All @@ -156,7 +156,11 @@ private void loadTimeZoneMappings()
// 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.
//
// This workaround seems required only for Connector/J 5.x (com.mysql.jdbc.TimeUtil),
// not necessary for Connector/J 8.x (com.mysql.cj.util.TimeUtil).
// TODO: Clarify for Connector/J 8.x just in case.
Field f = null;
try {
Class<?> timeUtilClass = Class.forName("com.mysql.jdbc.TimeUtil");
Expand All @@ -172,7 +176,16 @@ private void loadTimeZoneMappings()
f.set(null, timeZoneMappings);
}
}
catch (ClassNotFoundException | IllegalAccessException | NoSuchFieldException | IOException e) {
catch (ClassNotFoundException e) {
try {
Class.forName("com.mysql.cj.util.TimeUtil");
} catch (final ClassNotFoundException ex2) {
// Throw if neither the Connector/J 5.x nor 8.x driver is found.
throw new RuntimeException(e);
}
}
// Pass-through if the Connector/J 8.x driver is found. }
catch (IllegalAccessException | NoSuchFieldException | IOException e) {
throw new RuntimeException(e);
}
finally {
Expand Down