Skip to content

Commit 6a27ea7

Browse files
committed
Move JDBC driver loading to static initializer
1 parent 8621d1c commit 6a27ea7

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

src/main/java/com/alipay/oceanbase/rpc/location/LocationUtil.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public class LocationUtil {
6161
.getLogger(LocationUtil.class);
6262
static {
6363
ParserConfig.getGlobalInstance().setSafeMode(true);
64+
loadJdbcDriver();
6465
}
6566

6667
private static final String OB_VERSION_SQL = "SELECT /*+READ_CONSISTENCY(WEAK)*/ OB_VERSION() AS CLUSTER_VERSION;";
@@ -301,7 +302,6 @@ private static String formatObServerUrl(ObServerAddr obServerAddr, long connectT
301302
*/
302303
private static Connection getMetaRefreshConnection(String url, ObUserAuth sysUA)
303304
throws ObTableEntryRefreshException {
304-
loadJdbcDriver();
305305

306306
try {
307307
return DriverManager.getConnection(url, sysUA.getUserName(), sysUA.getPassword());
@@ -312,26 +312,20 @@ private static Connection getMetaRefreshConnection(String url, ObUserAuth sysUA)
312312
}
313313

314314
private static void loadJdbcDriver() {
315-
if (DRIVER_LOADED.get()) return;
316-
317-
synchronized (LocationUtil.class) {
318-
if (DRIVER_LOADED.get()) return;
319-
320-
try {
321-
Class.forName("com.mysql.cj.jdbc.Driver");
322-
DRIVER_LOADED.set(true);
323-
return;
324-
} catch (ClassNotFoundException ignored) {
325-
RUNTIME.debug("MySQL CJ driver not available");
326-
}
315+
try {
316+
Class.forName("com.mysql.cj.jdbc.Driver");
317+
return;
318+
} catch (ClassNotFoundException ignored) {
319+
RUNTIME.debug("Class 'com.mysql.cj.jdbc.Driver' not found, "
320+
+ "try to load legacy driver class 'com.mysql.jdbc.Driver'");
321+
}
327322

328-
try {
329-
Class.forName("com.mysql.jdbc.Driver");
330-
DRIVER_LOADED.set(true);
331-
} catch (ClassNotFoundException e) {
332-
RUNTIME.error(LCD.convert("01-00006"), "No JDBC driver found");
333-
throw new ObTableEntryRefreshException("No JDBC driver available", e);
334-
}
323+
try {
324+
Class.forName("com.mysql.jdbc.Driver");
325+
} catch (ClassNotFoundException e) {
326+
RUNTIME.error(LCD.convert("01-00006"), e.getMessage(), e);
327+
throw new ObTableEntryRefreshException(format("fail to find jdbc driver, errMsg=%s",
328+
e.getMessage()), e);
335329
}
336330
}
337331

0 commit comments

Comments
 (0)