Skip to content

Commit 2ec66e3

Browse files
committed
[MySQL] Support Connector/J 8.X
1 parent e9008d2 commit 2ec66e3

File tree

1 file changed

+49
-12
lines changed

1 file changed

+49
-12
lines changed

embulk-input-mysql/src/main/java/org/embulk/input/MySQLInputPlugin.java

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
package org.embulk.input;
22

3-
import java.io.IOException;
4-
import java.lang.reflect.Field;
5-
import java.util.Properties;
6-
import java.sql.Connection;
7-
import java.sql.DriverManager;
8-
import java.sql.SQLException;
9-
import java.util.Optional;
10-
113
import com.google.common.base.Throwables;
12-
134
import org.embulk.config.Config;
145
import org.embulk.config.ConfigDefault;
6+
import org.embulk.config.ConfigException;
157
import org.embulk.input.jdbc.AbstractJdbcInputPlugin;
168
import org.embulk.input.jdbc.Ssl;
179
import org.embulk.input.jdbc.getter.ColumnGetterFactory;
@@ -21,6 +13,16 @@
2113
import org.slf4j.Logger;
2214
import org.slf4j.LoggerFactory;
2315

16+
import java.io.IOException;
17+
import java.lang.reflect.Field;
18+
import java.sql.Connection;
19+
import java.sql.DriverManager;
20+
import java.sql.SQLException;
21+
import java.util.Arrays;
22+
import java.util.List;
23+
import java.util.Optional;
24+
import java.util.Properties;
25+
2426
public class MySQLInputPlugin
2527
extends AbstractJdbcInputPlugin
2628
{
@@ -158,20 +160,20 @@ private void loadTimeZoneMappings()
158160
// Here implements a workaround as as workaround.
159161
Field f = null;
160162
try {
161-
Class<?> timeUtilClass = Class.forName("com.mysql.jdbc.TimeUtil");
163+
Class<?> timeUtilClass = getTimeUtilClass();
162164
f = timeUtilClass.getDeclaredField("timeZoneMappings");
163165
f.setAccessible(true);
164166

165167
Properties timeZoneMappings = (Properties) f.get(null);
166168
if (timeZoneMappings == null) {
167169
timeZoneMappings = new Properties();
168170
synchronized (timeUtilClass) {
169-
timeZoneMappings.load(this.getClass().getResourceAsStream("/com/mysql/jdbc/TimeZoneMapping.properties"));
171+
loadTimezoneMappings(timeZoneMappings);
170172
}
171173
f.set(null, timeZoneMappings);
172174
}
173175
}
174-
catch (ClassNotFoundException | IllegalAccessException | NoSuchFieldException | IOException e) {
176+
catch (IllegalAccessException | NoSuchFieldException e) {
175177
throw Throwables.propagate(e);
176178
}
177179
finally {
@@ -181,4 +183,39 @@ private void loadTimeZoneMappings()
181183
}
182184
}
183185

186+
private Class<?> getTimeUtilClass() {
187+
Class<?> timeUtilClass;
188+
List<String> timeUtilClassNames = Arrays.asList(
189+
"com.mysql.jdbc.TimeUtil", // Connector/J v5.X
190+
"com.mysql.cj.util.TimeUtil" // Connector/J v8.X
191+
);
192+
193+
for (String name : timeUtilClassNames) {
194+
try {
195+
timeUtilClass = Class.forName(name);
196+
return timeUtilClass;
197+
} catch (ClassNotFoundException e) {
198+
// do nothing.
199+
}
200+
}
201+
throw new ConfigException("Can't find TimeUtil Class");
202+
}
203+
204+
private void loadTimezoneMappings(Properties timeZoneMappings) {
205+
List<String> timeZoneResources = Arrays.asList(
206+
"/com/mysql/jdbc/TimeZoneMapping.properties", // Connector/J v5.X
207+
"/com/mysql/cj/util/TimeZoneMapping.properties" // Connector/J v8.X
208+
);
209+
210+
for (String resource : timeZoneResources) {
211+
try {
212+
timeZoneMappings.load(this.getClass().getResourceAsStream(resource));
213+
return;
214+
} catch (IOException | NullPointerException e) {
215+
// do nothing.
216+
}
217+
}
218+
throw new ConfigException("Can't find Timezone mapping property file.");
219+
}
220+
184221
}

0 commit comments

Comments
 (0)