Skip to content

Commit 540e82d

Browse files
authored
feat: support to use mysql driver 5.x (#148)
* add fallback to load legacy mysql driver * update it case
1 parent fb86380 commit 540e82d

File tree

7 files changed

+88
-189
lines changed

7 files changed

+88
-189
lines changed

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -267,16 +267,7 @@ private static String formatObServerUrl(ObServerAddr obServerAddr, long connectT
267267
*/
268268
private static Connection getMetaRefreshConnection(String url, ObUserAuth sysUA)
269269
throws ObTableEntryRefreshException {
270-
try {
271-
Class.forName("com.mysql.cj.jdbc.Driver");
272-
} catch (ClassNotFoundException e) {
273-
RUNTIME.error(LCD.convert("01-00006"), e.getMessage(), e);
274-
throw new ObTableEntryRefreshException(format(
275-
"fail to find com.mysql.cj.jdbc.Driver, errMsg=%s", e.getMessage()), e);
276-
} catch (Exception e) {
277-
RUNTIME.error(LCD.convert("01-00005"), e.getMessage(), e);
278-
throw new ObTableEntryRefreshException("fail to decode proxyro password", e);
279-
}
270+
loadJdbcDriver();
280271

281272
try {
282273
return DriverManager.getConnection(url, sysUA.getUserName(), sysUA.getPassword());
@@ -286,6 +277,24 @@ private static Connection getMetaRefreshConnection(String url, ObUserAuth sysUA)
286277
}
287278
}
288279

280+
private static void loadJdbcDriver() {
281+
try {
282+
Class.forName("com.mysql.cj.jdbc.Driver");
283+
return;
284+
} catch (ClassNotFoundException e) {
285+
RUNTIME.info("Class 'com.mysql.cj.jdbc.Driver' not found, "
286+
+ "try to load legacy driver class 'com.mysql.jdbc.Driver'");
287+
}
288+
289+
try {
290+
Class.forName("com.mysql.jdbc.Driver");
291+
} catch (ClassNotFoundException e) {
292+
RUNTIME.error(LCD.convert("01-00006"), e.getMessage(), e);
293+
throw new ObTableEntryRefreshException(format("fail to find jdbc driver, errMsg=%s",
294+
e.getMessage()), e);
295+
}
296+
}
297+
289298
/*
290299
* Refresh server LDC info.
291300
*

src/main/resources/oceanbase-table-client/log-codes.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
01-00003=Exception caught!
44
01-00004=close obTable {} error
55
01-00005=fail to decode proxyro password, errMsg={}
6-
01-00006=fail to find com.mysql.cj.jdbc.Driver, key={}, errMsg={}
6+
01-00006=fail to find jdbc driver, key={}, errMsg={}
77
01-00007=fail to refresh table entry from remote url={}, key={}
88
01-00008=table entry is invalid, addr = {}, key = {}, entry = {}
99
01-00009=fail to get table entry from remote, key={}

src/test/java/com/alipay/oceanbase/rpc/ObTableClientITCase.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,20 @@
2121
import com.alipay.oceanbase.rpc.property.Property;
2222
import com.alipay.oceanbase.rpc.table.api.Table;
2323
import com.alipay.oceanbase.rpc.util.ObTableClientTestUtil;
24+
2425
import org.junit.Test;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
2528

2629
public class ObTableClientITCase extends ContainerTestBase {
2730

28-
public Table client;
31+
private static final Logger logger = LoggerFactory.getLogger(ObTableClientITCase.class);
32+
33+
public Table client;
2934

3035
@Test
3136
public void testAll() throws Exception {
32-
if (!ObTableClientTestUtil.FULL_USER_NAME.equals("root@test#obcluster")) {
37+
if (!ObTableClientTestUtil.FULL_USER_NAME.equals(TEST_USERNAME)) {
3338
return;
3439
}
3540
final ObTableClient obTableClient = ObTableClientTestUtil.newTestClient();
@@ -45,7 +50,7 @@ public void testAll() throws Exception {
4550
obTableClient.addProperty(Property.SERVER_ENABLE_REROUTING.getKey(), "False");
4651
obTableClient.init();
4752

48-
System.out.println("obTableClient init success");
53+
logger.info("obTableClient init success");
4954

5055
// ObTableClientTest
5156
ObTableClientTest obTableClientTest = new ObTableClientTest();
@@ -56,8 +61,8 @@ public void testAll() throws Exception {
5661
obTableClientTest.testCompareWithNull();
5762
obTableClientTest.testQueryFilterLimit();
5863
// Todo: add more test
59-
System.out.println("ObTableClientTest success");
64+
logger.info("ObTableClientTest success");
6065

61-
System.out.println("testAll success");
66+
logger.info("testAll success");
6267
}
6368
}

src/test/java/com/alipay/oceanbase/rpc/containerBase/ContainerTestBase.java

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,44 +34,63 @@
3434
package com.alipay.oceanbase.rpc.containerBase;
3535

3636
import com.alipay.oceanbase.rpc.util.ObTableClientTestUtil;
37-
import org.junit.After;
38-
import org.junit.Before;
39-
import org.testcontainers.lifecycle.Startables;
37+
38+
import org.junit.BeforeClass;
39+
import org.junit.ClassRule;
40+
import org.slf4j.Logger;
41+
import org.slf4j.LoggerFactory;
42+
import org.testcontainers.containers.GenericContainer;
43+
import org.testcontainers.containers.Network;
44+
import org.testcontainers.containers.output.Slf4jLogConsumer;
45+
import org.testcontainers.containers.wait.strategy.Wait;
4046
import org.testcontainers.utility.MountableFile;
4147

48+
import java.time.Duration;
49+
import java.util.Arrays;
50+
4251
public class ContainerTestBase {
4352

44-
protected OceanBaseContainer obServer;
45-
protected String imageTag = "4.2.1_bp2";
53+
private static final Logger logger = LoggerFactory
54+
.getLogger(ContainerTestBase.class);
4655

47-
@Before
48-
public void before() {
49-
if (!ObTableClientTestUtil.FULL_USER_NAME.equals("full-user-name")) {
50-
return;
51-
}
56+
protected static final Network NETWORK = Network.newNetwork();
57+
protected static final String CLUSTER_NAME = "obkv-table-client-java";
58+
protected static final String SYS_PASSWORD = "OB_ROOT_PASSWORD";
59+
protected static final String TEST_USERNAME = "root@test#" + CLUSTER_NAME;
5260

53-
obServer = new OceanBaseContainer(OceanBaseContainer.DOCKER_IMAGE_NAME + ":" + imageTag)
54-
.withNetworkMode("host")
55-
.withSysPassword("OB_ROOT_PASSWORD")
56-
.withCopyFileToContainer(MountableFile.forClasspathResource("ci.sql"),
57-
"/root/boot/init.d/init.sql");
61+
@ClassRule
62+
public static final GenericContainer<?> OB_SERVER = createOceanBaseCEContainer();
5863

59-
Startables.deepStart(obServer).join();
64+
@SuppressWarnings({ "unchecked", "rawtypes" })
65+
protected static GenericContainer createOceanBaseCEContainer() {
66+
GenericContainer container = new GenericContainer("oceanbase/oceanbase-ce:4.2.1_bp2")
67+
.withNetwork(NETWORK)
68+
.withExposedPorts(2881, 2882, 8080)
69+
.withEnv("MODE", "slim")
70+
.withEnv("OB_CLUSTER_NAME", CLUSTER_NAME)
71+
.withEnv("OB_ROOT_PASSWORD", SYS_PASSWORD)
72+
.withCopyFileToContainer(MountableFile.forClasspathResource("ci.sql"),
73+
"/root/boot/init.d/init.sql")
74+
.waitingFor(
75+
Wait.forLogMessage(".*boot success!.*", 1)
76+
.withStartupTimeout(Duration.ofMinutes(5)))
77+
.withLogConsumer(new Slf4jLogConsumer(logger));
78+
container.setPortBindings(Arrays.asList("2881:2881", "2882:2882", "8080:8080"));
79+
return container;
80+
}
6081

61-
System.out.println("observer start success");
82+
@BeforeClass
83+
public static void before() {
84+
if (!ObTableClientTestUtil.FULL_USER_NAME.equals("full-user-name")) {
85+
return;
86+
}
6287

6388
// Set config
64-
ObTableClientTestUtil.PARAM_URL = "http://127.0.0.1:8080/services?Action=ObRootServiceInfo&ObCluster=obcluster&database=test";
65-
ObTableClientTestUtil.FULL_USER_NAME = "root@test#obcluster";
89+
ObTableClientTestUtil.PARAM_URL = "http://127.0.0.1:8080/services?Action=ObRootServiceInfo&ObCluster="
90+
+ CLUSTER_NAME + "&database=test";
91+
ObTableClientTestUtil.FULL_USER_NAME = TEST_USERNAME;
6692
ObTableClientTestUtil.PASSWORD = "";
6793
ObTableClientTestUtil.PROXY_SYS_USER_NAME = "root";
68-
ObTableClientTestUtil.PROXY_SYS_USER_PASSWORD = "OB_ROOT_PASSWORD";
69-
}
70-
71-
@After
72-
public void after() {
73-
if (obServer != null) {
74-
obServer.close();
75-
}
94+
ObTableClientTestUtil.PROXY_SYS_USER_PASSWORD = SYS_PASSWORD;
7695
}
7796
}

src/test/java/com/alipay/oceanbase/rpc/containerBase/OceanBaseContainer.java

Lines changed: 0 additions & 140 deletions
This file was deleted.

src/test/resources/log4j.properties

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{50} - %msg%n</pattern>
6+
</encoder>
7+
</appender>
8+
9+
<root level="INFO">
10+
<appender-ref ref="STDOUT"/>
11+
</root>
12+
</configuration>

0 commit comments

Comments
 (0)