mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
support mysql_driver_6.0.x
This commit is contained in:
+4
-1
@@ -45,7 +45,10 @@ public class MySqlValidConnectionChecker extends ValidConnectionCheckerAdapter i
|
||||
public MySqlValidConnectionChecker(){
|
||||
try {
|
||||
clazz = Utils.loadClass("com.mysql.jdbc.MySQLConnection");
|
||||
ping = clazz.getMethod("pingInternal", boolean.class, int.class);
|
||||
if (clazz != null) {
|
||||
ping = clazz.getMethod("pingInternal", boolean.class, int.class);
|
||||
}
|
||||
|
||||
if (ping != null) {
|
||||
usePingMethod = true;
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ public class DruidXADataSource extends DruidDataSource implements XADataSource {
|
||||
}
|
||||
|
||||
if (JdbcUtils.MYSQL.equals(dbType)) {
|
||||
return MySqlUtils.createXAConnection(physicalConn);
|
||||
return MySqlUtils.createXAConnection(driver, physicalConn);
|
||||
}
|
||||
|
||||
if (JdbcUtils.POSTGRESQL.equals(dbType)) {
|
||||
|
||||
@@ -15,28 +15,75 @@
|
||||
*/
|
||||
package com.alibaba.druid.util;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.sql.XAConnection;
|
||||
|
||||
import com.mysql.jdbc.Util;
|
||||
import com.mysql.jdbc.jdbc2.optional.MysqlXAConnection;
|
||||
import com.mysql.jdbc.jdbc2.optional.SuspendableXAConnection;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.sql.Connection;
|
||||
import java.sql.Driver;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLFeatureNotSupportedException;
|
||||
|
||||
public class MySqlUtils {
|
||||
static Class<?> utilClass;
|
||||
static boolean utilClassError = false;
|
||||
static boolean utilClass_isJdbc4 = false;
|
||||
|
||||
public static XAConnection createXAConnection(Connection physicalConn) throws SQLException {
|
||||
com.mysql.jdbc.Connection mysqlConn = (com.mysql.jdbc.Connection)physicalConn;
|
||||
if(mysqlConn.getPinGlobalTxToPhysicalConnection()) {
|
||||
static Class<?> connectionClass = null;
|
||||
static Method getPinGlobalTxToPhysicalConnectionMethod = null;
|
||||
|
||||
if (!Util.isJdbc4()) {
|
||||
return new SuspendableXAConnection(mysqlConn);
|
||||
}
|
||||
static Class<?> suspendableXAConnectionClass = null;
|
||||
static Constructor<?> suspendableXAConnectionConstructor = null;
|
||||
|
||||
return new com.mysql.jdbc.jdbc2.optional.JDBC4SuspendableXAConnection(mysqlConn);
|
||||
|
||||
}
|
||||
return new MysqlXAConnection(mysqlConn, false);
|
||||
static Class<?> JDBC4SuspendableXAConnectionClass = null;
|
||||
static Constructor<?> JDBC4SuspendableXAConnectionConstructor = null;
|
||||
|
||||
static Class<?> MysqlXAConnectionClass = null;
|
||||
static Constructor<?> MysqlXAConnectionConstructor = null;
|
||||
|
||||
public static XAConnection createXAConnection(Driver driver, Connection physicalConn) throws SQLException {
|
||||
if (driver.getMajorVersion() == 5) {
|
||||
if (utilClass == null && !utilClassError) {
|
||||
try {
|
||||
utilClass = Class.forName("com.mysql.jdbc.Util");
|
||||
|
||||
Method method = utilClass.getMethod("isJdbc4");
|
||||
utilClass_isJdbc4 = (Boolean) method.invoke(null);
|
||||
|
||||
connectionClass = Class.forName("com.mysql.jdbc.Connection");
|
||||
getPinGlobalTxToPhysicalConnectionMethod = connectionClass.getMethod("getPinGlobalTxToPhysicalConnection");
|
||||
|
||||
suspendableXAConnectionClass = Class.forName("com.mysql.jdbc.jdbc2.optional.SuspendableXAConnection");
|
||||
suspendableXAConnectionConstructor = suspendableXAConnectionClass.getConstructor(connectionClass);
|
||||
|
||||
JDBC4SuspendableXAConnectionClass = Class.forName("com.mysql.jdbc.jdbc2.optional.JDBC4SuspendableXAConnection");
|
||||
JDBC4SuspendableXAConnectionConstructor = JDBC4SuspendableXAConnectionClass.getConstructor(connectionClass);
|
||||
|
||||
MysqlXAConnectionClass = Class.forName("com.mysql.jdbc.jdbc2.optional.MysqlXAConnection");
|
||||
MysqlXAConnectionConstructor = MysqlXAConnectionClass.getConstructor(connectionClass, boolean.class);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
utilClassError = true;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
boolean pinGlobTx = (Boolean) getPinGlobalTxToPhysicalConnectionMethod.invoke(physicalConn);
|
||||
if (pinGlobTx) {
|
||||
if (!utilClass_isJdbc4) {
|
||||
return (XAConnection) suspendableXAConnectionConstructor.newInstance(physicalConn);
|
||||
}
|
||||
|
||||
return (XAConnection) JDBC4SuspendableXAConnectionConstructor.newInstance(physicalConn);
|
||||
}
|
||||
|
||||
return (XAConnection) MysqlXAConnectionConstructor.newInstance(physicalConn, Boolean.FALSE);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
throw new SQLFeatureNotSupportedException();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user