improved mysql driver 6.0 support.

This commit is contained in:
wenshao
2017-12-02 15:13:07 +08:00
parent 316f59e3d2
commit df8d99b468
6 changed files with 205 additions and 41 deletions
@@ -2,9 +2,9 @@ package com.alibaba.druid.bvt.pool.vendor;
import com.alibaba.druid.PoolTestCase;
import com.alibaba.druid.pool.vendor.MySqlExceptionSorter;
import com.mysql.jdbc.CommunicationsException;
import junit.framework.TestCase;
import com.alibaba.druid.util.MySqlUtils;
import java.lang.reflect.Constructor;
import java.sql.SQLException;
/**
@@ -14,12 +14,40 @@ public class MySqlExceptionSorterTest_1 extends PoolTestCase {
public void test_0() throws Exception {
MySqlExceptionSorter sorter = new MySqlExceptionSorter();
SQLException rootException = new SQLException(
new SQLException("Could not retrieve transation read-only status server",
new CommunicationsException(null, 0, 0, null)
)
);
Class clazz = MySqlUtils.getCommunicationsExceptionClass();
assertTrue(sorter.isExceptionFatal(rootException));
if ("com.mysql.jdbc.CommunicationsException".equals(clazz.getName())) {
Constructor constructor = null;
for (Constructor item : clazz.getConstructors()) {
if (item.getParameterTypes().length == 4) {
constructor = item;
break;
}
}
SQLException rootException = new SQLException(
new SQLException("Could not retrieve transation read-only status server",
(Exception) constructor.newInstance(null, 0, 0, null)
)
);
assertTrue(sorter.isExceptionFatal(rootException));
} else {
Constructor constructor = null;
for (Constructor item : clazz.getConstructors()) {
if (item.getParameterTypes().length == 2) {
constructor = item;
break;
}
}
SQLException rootException = new SQLException(
new SQLException("Could not retrieve transation read-only status server",
(Exception) constructor.newInstance(null, null)
)
);
assertTrue(sorter.isExceptionFatal(rootException));
}
}
}
@@ -2,9 +2,10 @@ package com.alibaba.druid.bvt.pool.vendor;
import com.alibaba.druid.PoolTestCase;
import com.alibaba.druid.pool.vendor.MySqlExceptionSorter;
import com.mysql.jdbc.CommunicationsException;
import com.alibaba.druid.util.MySqlUtils;
import junit.framework.TestCase;
import java.lang.reflect.Constructor;
import java.net.SocketTimeoutException;
import java.sql.SQLException;
@@ -76,7 +76,13 @@ public class DruidDriverTest extends TestCase {
}
public void test_getRawDriverClassName() throws Exception {
Assert.assertEquals("com.mysql.jdbc.Driver", JdbcUtils.getDriverClassName("jdbc:mysql:"));
String className = JdbcUtils.getDriverClassName("jdbc:mysql:");
if (className.equals("com.mysql.jdbc.Driver")) {
assertEquals("com.mysql.jdbc.Driver", JdbcUtils.getDriverClassName("jdbc:mysql:"));
} else {
assertEquals("com.mysql.cj.jdbc.Driver", JdbcUtils.getDriverClassName("jdbc:mysql:"));
}
Assert.assertEquals("oracle.jdbc.OracleDriver", JdbcUtils.getDriverClassName("jdbc:oracle:"));
Assert.assertEquals("com.microsoft.jdbc.sqlserver.SQLServerDriver",
JdbcUtils.getDriverClassName("jdbc:microsoft:"));
@@ -19,7 +19,11 @@ public class MySqlUtilsTest extends TestCase {
Connection conn = (Connection) constructor.newInstance();
MySqlUtils.createXAConnection(driver, conn);
} else if (majorVersion == 6) {
Class<?> clazz_ConnectionImpl = Class.forName("com.mysql.cj.jdbc.ConnectionImpl");
Constructor<?> constructor = clazz_ConnectionImpl.getDeclaredConstructor();
constructor.setAccessible(true);
Connection conn = (Connection) constructor.newInstance();
MySqlUtils.createXAConnection(driver, conn);
}
}
}