Skip to content

Commit 8621d1c

Browse files
committed
Optimize JDBC driver loading with thread-safe initialization and reduce log
1 parent 2ebc250 commit 8621d1c

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.sql.*;
4444
import java.text.MessageFormat;
4545
import java.util.*;
46+
import java.util.concurrent.atomic.AtomicBoolean;
4647

4748
import static com.alipay.oceanbase.rpc.location.model.partition.ObPartitionKey.MAX_PARTITION_ELEMENT;
4849
import static com.alipay.oceanbase.rpc.location.model.partition.ObPartitionKey.MIN_PARTITION_ELEMENT;
@@ -216,6 +217,8 @@ public class LocationUtil {
216217
private static final int MAX_TABLET_NUMS_EPOCH = Integer.parseInt(System.getProperty("max.table.num.epoch","300"));
217218
private static final int TABLE_ENTRY_LOCATION_REFRESH_THRESHOLD = Integer.parseInt(System.getProperty("table.entry.location.refresh.threshold","0"));
218219

220+
private static final AtomicBoolean DRIVER_LOADED = new AtomicBoolean();
221+
219222
private abstract static class TableEntryRefreshWithPriorityCallback<T> {
220223
abstract T execute(ObServerAddr obServerAddr) throws ObTableEntryRefreshException;
221224
}
@@ -309,20 +312,26 @@ private static Connection getMetaRefreshConnection(String url, ObUserAuth sysUA)
309312
}
310313

311314
private static void loadJdbcDriver() {
312-
try {
313-
Class.forName("com.mysql.cj.jdbc.Driver");
314-
return;
315-
} catch (ClassNotFoundException e) {
316-
RUNTIME.info("Class 'com.mysql.cj.jdbc.Driver' not found, "
317-
+ "try to load legacy driver class 'com.mysql.jdbc.Driver'");
318-
}
315+
if (DRIVER_LOADED.get()) return;
319316

320-
try {
321-
Class.forName("com.mysql.jdbc.Driver");
322-
} catch (ClassNotFoundException e) {
323-
RUNTIME.error(LCD.convert("01-00006"), e.getMessage(), e);
324-
throw new ObTableEntryRefreshException(format("fail to find jdbc driver, errMsg=%s",
325-
e.getMessage()), e);
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+
}
327+
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+
}
326335
}
327336
}
328337

0 commit comments

Comments
 (0)