Browse Source

REPORT-70827 & REPORT-70810 兼容性问题

bugfix/KERNEL-11409-jackson
rinoux 3 years ago
parent
commit
30d122e9a0
  1. 7
      fine-druid/readme.MD
  2. 156
      fine-druid/src/main/java/com/fr/third/alibaba/druid/pool/DruidDataSourceFactory.java
  3. 110
      fine-druid/src/main/java/com/fr/third/alibaba/druid/util/MySqlUtils.java
  4. 14
      fine-druid/src/main/java/com/fr/third/alibaba/druid/util/Utils.java

7
fine-druid/readme.MD

@ -5,9 +5,12 @@
## 变动列表
> 由于需要适配数据源的一些特性和默认配置,对Druid有部分修改,后续更新都要保留下列修改
| 版本 | 日期 | FineReport变动 |
|-------|---|-------------------------------------------------------------------------------------|
|-------|------------|-------------------------------------------------------------------------------------|
| 1.2.9 | 2022-04-27 | DruidAbstractDataSource.testConnectionInternal()判断timeBetweenEvictionRunsMillis > 0 |
| 1.2.9 | 2022-04-27 | MysqlUtils.getLastPacketReceivedTimeMs添加驱动判断 |
| 1.2.9 | 2022-05-05 | MysqlUtils.getLastPacketReceivedTimeMs根据类加载器区分连接实现等,不在使用全局变量 |
| 1.2.9 | 2022-05-05 | com.fr.third.alibaba.druid.util.Utils.loadClass改为优先从线程类加载器加载类 |
| 1.2.9 | 2022-05-05 | 恢复com.fr.third.alibaba.druid.pool.DruidDataSourceFactory对hibernate配置属性的支持 |

156
fine-druid/src/main/java/com/fr/third/alibaba/druid/pool/DruidDataSourceFactory.java

@ -39,27 +39,46 @@ public class DruidDataSourceFactory implements ObjectFactory {
static final int UNKNOWN_TRANSACTIONISOLATION = -1;
private final static String HIBERNATE_PREFIX = "hibernate.";
private final static String HIBERNATE_CONN_PREFIX = "connection.";
public final static String PROP_DEFAULTAUTOCOMMIT = "defaultAutoCommit";
public final static String PROP_DEFAULTREADONLY = "defaultReadOnly";
public final static String PROP_DEFAULTTRANSACTIONISOLATION = "defaultTransactionIsolation";
public final static String PROP_DEFAULTCATALOG = "defaultCatalog";
public final static String PROP_DRIVERCLASSNAME = "driverClassName";
public final static String HB_PROP_DRIVERCLASSNAME = HIBERNATE_PREFIX + HIBERNATE_CONN_PREFIX + "driver_class";
public final static String PROP_MAXACTIVE = "maxActive";
public final static String HB_PROP_MAXACTIVE = HIBERNATE_PREFIX + "maxActive";
public final static String PROP_MAXIDLE = "maxIdle";
public final static String HB_PROP_MAXIDLE = HIBERNATE_PREFIX + "maxIdle";
public final static String PROP_MINIDLE = "minIdle";
public final static String HB_PROP_MINIDLE = HIBERNATE_PREFIX + "minIdle";
public final static String PROP_INITIALSIZE = "initialSize";
public final static String HB_PROP_INITIALSIZE = HIBERNATE_PREFIX + "initialSize";
public final static String PROP_MAXWAIT = "maxWait";
public final static String HB_PROP_MAXWAIT = HIBERNATE_PREFIX + "maxWait";
public final static String PROP_TESTONBORROW = "testOnBorrow";
public final static String HB_PROP_TESTONBORROW = HIBERNATE_PREFIX + "testOnBorrow";
public final static String PROP_TESTONRETURN = "testOnReturn";
public final static String HB_PROP_TESTONRETURN = HIBERNATE_PREFIX + "testOnReturn";
public final static String PROP_TIMEBETWEENEVICTIONRUNSMILLIS = "timeBetweenEvictionRunsMillis";
public final static String HB_PROP_TIMEBETWEENEVICTIONRUNSMILLIS = HIBERNATE_PREFIX + "timeBetweenEvictionRunsMillis";
public final static String PROP_NUMTESTSPEREVICTIONRUN = "numTestsPerEvictionRun";
public final static String HB_PROP_NUMTESTSPEREVICTIONRUN = HIBERNATE_PREFIX + "numTestsPerEvictionRun";
public final static String PROP_MINEVICTABLEIDLETIMEMILLIS = "minEvictableIdleTimeMillis";
public final static String HB_PROP_MINEVICTABLEIDLETIMEMILLIS = HIBERNATE_PREFIX + "minEvictableIdleTimeMillis";
public final static String PROP_PHY_TIMEOUT_MILLIS = "phyTimeoutMillis";
public final static String PROP_TESTWHILEIDLE = "testWhileIdle";
public final static String HB_PROP_TESTWHILEIDLE = HIBERNATE_PREFIX + "testWhileIdle";
public final static String PROP_PASSWORD = "password";
public final static String HB_PROP_PASSWORD = HIBERNATE_PREFIX + HIBERNATE_CONN_PREFIX + "password";
public final static String PROP_URL = "url";
public final static String HB_PROP_URL = HIBERNATE_PREFIX + HIBERNATE_CONN_PREFIX + "url";
public final static String PROP_USERNAME = "username";
public final static String HB_PROP_USERNAME = HIBERNATE_PREFIX + HIBERNATE_CONN_PREFIX + "username";
public final static String PROP_VALIDATIONQUERY = "validationQuery";
public final static String HB_PROP_VALIDATIONQUERY = HIBERNATE_PREFIX + "validationQuery";
public final static String PROP_VALIDATIONQUERY_TIMEOUT = "validationQueryTimeout";
public final static String PROP_INITCONNECTIONSQLS = "initConnectionSqls";
public final static String PROP_ACCESSTOUNDERLYINGCONNECTIONALLOWED = "accessToUnderlyingConnectionAllowed";
@ -223,56 +242,161 @@ public class DruidDataSourceFactory implements ObjectFactory {
value = (String) properties.get(PROP_DRIVERCLASSNAME);
if (value != null) {
dataSource.setDriverClassName(value);
} else {
value = (String) properties.get(HB_PROP_DRIVERCLASSNAME);
if (value != null) {
dataSource.setDriverClassName(value);
}
}
value = (String) properties.get(PROP_MAXACTIVE);
if (value != null) {
dataSource.setMaxActive(Integer.parseInt(value));
} else {
Object o = properties.get(HB_PROP_MAXACTIVE);
if (o != null) {
if (o instanceof Integer) {
dataSource.setMaxActive((Integer) o);
} else {
value = (String) o;
dataSource.setMaxActive(Integer.parseInt(value));
}
}
}
value = (String) properties.get(PROP_MAXIDLE);
if (value != null) {
dataSource.setMaxIdle(Integer.parseInt(value));
} else {
Object o = properties.get(HB_PROP_MAXIDLE);
if (o != null) {
if (o instanceof Integer) {
dataSource.setMaxIdle((Integer) o);
} else {
value = (String) o;
dataSource.setMaxIdle(Integer.parseInt(value));
}
}
}
value = (String) properties.get(PROP_MINIDLE);
if (value != null) {
dataSource.setMinIdle(Integer.parseInt(value));
} else {
Object o = properties.get(HB_PROP_MINIDLE);
if (o != null) {
if (o instanceof Integer) {
dataSource.setMinIdle((Integer) o);
} else {
value = (String) o;
dataSource.setMinIdle(Integer.parseInt(value));
}
}
}
value = (String) properties.get(PROP_INITIALSIZE);
if (value != null) {
dataSource.setInitialSize(Integer.parseInt(value));
} else {
Object o = properties.get(HB_PROP_INITIALSIZE);
if (o != null) {
if (o instanceof Integer) {
dataSource.setInitialSize((Integer) o);
} else {
value = (String) o;
dataSource.setInitialSize(Integer.parseInt(value));
}
}
}
value = (String) properties.get(PROP_MAXWAIT);
if (value != null) {
dataSource.setMaxWait(Long.parseLong(value));
} else {
Object o = properties.get(HB_PROP_MAXWAIT);
if (o != null) {
if (o instanceof Integer) {
dataSource.setMaxWait((Integer) o);
} else {
value = (String) o;
dataSource.setMaxWait(Integer.parseInt(value));
}
}
}
value = (String) properties.get(PROP_TESTONBORROW);
if (value != null) {
dataSource.setTestOnBorrow(Boolean.valueOf(value).booleanValue());
} else {
Object o = properties.get(HB_PROP_TESTONBORROW);
if (o != null) {
if (o instanceof Boolean) {
dataSource.setTestOnBorrow((Boolean) o);
} else {
value = (String) o;
dataSource.setTestOnBorrow(Boolean.valueOf(value).booleanValue());
}
}
}
value = (String) properties.get(PROP_TESTONRETURN);
if (value != null) {
dataSource.setTestOnReturn(Boolean.valueOf(value).booleanValue());
} else {
Object o = properties.get(HB_PROP_TESTONRETURN);
if (o != null) {
if (o instanceof Boolean) {
dataSource.setTestOnReturn((Boolean) o);
} else {
value = (String) o;
dataSource.setTestOnReturn(Boolean.valueOf(value).booleanValue());
}
}
}
value = (String) properties.get(PROP_TIMEBETWEENEVICTIONRUNSMILLIS);
if (value != null) {
dataSource.setTimeBetweenEvictionRunsMillis(Long.parseLong(value));
} else {
Object o = properties.get(HB_PROP_TIMEBETWEENEVICTIONRUNSMILLIS);
if (o != null) {
if (o instanceof Integer) {
dataSource.setTimeBetweenEvictionRunsMillis((Integer) o);
} else {
value = (String) o;
dataSource.setTimeBetweenEvictionRunsMillis(Integer.parseInt(value));
}
}
}
value = (String) properties.get(PROP_NUMTESTSPEREVICTIONRUN);
if (value != null) {
dataSource.setNumTestsPerEvictionRun(Integer.parseInt(value));
} else {
Object o = properties.get(HB_PROP_NUMTESTSPEREVICTIONRUN);
if (o != null) {
if (o instanceof Integer) {
dataSource.setNumTestsPerEvictionRun((Integer) o);
} else {
value = (String) o;
dataSource.setNumTestsPerEvictionRun(Integer.parseInt(value));
}
}
}
value = (String) properties.get(PROP_MINEVICTABLEIDLETIMEMILLIS);
if (value != null) {
dataSource.setMinEvictableIdleTimeMillis(Long.parseLong(value));
} else {
Object o = properties.get(HB_PROP_MINEVICTABLEIDLETIMEMILLIS);
if (o != null) {
if (o instanceof Integer) {
dataSource.setMinEvictableIdleTimeMillis((Integer) o);
} else {
value = (String) o;
dataSource.setMinEvictableIdleTimeMillis(Integer.parseInt(value));
}
}
}
value = (String) properties.get(PROP_PHY_TIMEOUT_MILLIS);
@ -283,27 +407,57 @@ public class DruidDataSourceFactory implements ObjectFactory {
value = (String) properties.get(PROP_TESTWHILEIDLE);
if (value != null) {
dataSource.setTestWhileIdle(Boolean.valueOf(value).booleanValue());
} else {
Object o = properties.get(HB_PROP_TESTWHILEIDLE);
if (o != null) {
if (o instanceof Boolean) {
dataSource.setTestWhileIdle((Boolean) o);
} else {
value = (String) o;
dataSource.setTestWhileIdle(Boolean.valueOf(value).booleanValue());
}
}
}
value = (String) properties.get(PROP_PASSWORD);
if (value != null) {
dataSource.setPassword(value);
} else {
value = (String) properties.get(HB_PROP_PASSWORD);
if (value != null) {
dataSource.setPassword(value);
}
}
value = (String) properties.get(PROP_URL);
if (value != null) {
dataSource.setUrl(value);
} else {
value = (String) properties.get(HB_PROP_URL);
if (value != null) {
dataSource.setUrl(value);
}
}
value = (String) properties.get(PROP_USERNAME);
if (value != null) {
dataSource.setUsername(value);
} else {
value = (String) properties.get(HB_PROP_USERNAME);
if (value != null) {
dataSource.setUsername(value);
}
}
value = (String) properties.get(PROP_VALIDATIONQUERY);
if (value != null) {
if (value != null && value.length() > 0) {
dataSource.setValidationQuery(value);
} else {
value = (String) properties.get(HB_PROP_VALIDATIONQUERY);
if (value != null && value.length() > 0) {
dataSource.setValidationQuery(value);
}
}
value = (String) properties.get(PROP_VALIDATIONQUERY_TIMEOUT);
if (value != null) {

110
fine-druid/src/main/java/com/fr/third/alibaba/druid/util/MySqlUtils.java

@ -45,8 +45,10 @@ import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
public class MySqlUtils {
static Class<?> utilClass;
@ -351,7 +353,7 @@ public class MySqlUtils {
return SQLUtils.toSQLString(stmtList, DbType.mysql);
}
private static transient Class class_connectionImpl = null;
/* private static transient Class class_connectionImpl = null;
private static transient boolean class_connectionImpl_Error = false;
private static transient Method method_getIO = null;
private static transient boolean method_getIO_error = false;
@ -363,7 +365,7 @@ public class MySqlUtils {
private volatile static boolean mysqlJdbcVersion6 = false;
private static transient Class classJdbc = null;
private static transient Method getIdleFor = null;
private static transient boolean getIdleForError = false;
private static transient boolean getIdleForError = false;*/
public static Long getId(Object conn) {
if (conn == null) {
@ -401,112 +403,140 @@ public class MySqlUtils {
}
public static long getLastPacketReceivedTimeMs(Connection conn) throws SQLException {
if (class_connectionImpl == null && !class_connectionImpl_Error) {
ClassLoader cl = conn.getClass().getClassLoader();
ConnectionClass cc = classLoaderConnectionClassMap.compute(cl, (k, v) -> {
if (v == null || v.class_connectionImpl.getClassLoader() != cl) {
return new ConnectionClass();
}
return v;
});
if (cc.class_connectionImpl == null && !cc.class_connectionImpl_Error) {
try {
class_connectionImpl = Utils.loadClass("com.mysql.jdbc.MySQLConnection");
if (class_connectionImpl == null || class_connectionImpl.getClassLoader() != conn.getClass().getClassLoader()) {
class_connectionImpl = Utils.loadClass("com.mysql.cj.MysqlConnection");
if (class_connectionImpl != null) {
mysqlJdbcVersion6 = true;
cc.class_connectionImpl = Utils.loadClass("com.mysql.jdbc.MySQLConnection");
if (cc.class_connectionImpl == null) {
cc.class_connectionImpl = Utils.loadClass("com.mysql.cj.MysqlConnection");
if (cc.class_connectionImpl != null) {
cc.mysqlJdbcVersion6 = true;
}
}
} catch (Throwable error) {
class_connectionImpl_Error = true;
cc.class_connectionImpl_Error = true;
}
}
if (class_connectionImpl == null) {
if (cc.class_connectionImpl == null) {
return -1;
}
if(mysqlJdbcVersion6){
if (classJdbc == null) {
classJdbc = Utils.loadClass("com.mysql.cj.jdbc.JdbcConnection");
if (cc.mysqlJdbcVersion6) {
if (cc.classJdbc == null) {
cc.classJdbc = Utils.loadClass("com.mysql.cj.jdbc.JdbcConnection");
}
if (classJdbc == null) {
if (cc.classJdbc == null) {
return -1;
}
if (getIdleFor == null && !getIdleForError) {
if (cc.getIdleFor == null && !cc.getIdleForError) {
try {
getIdleFor = classJdbc.getMethod("getIdleFor");
getIdleFor.setAccessible(true);
cc.getIdleFor = cc.classJdbc.getMethod("getIdleFor");
cc.getIdleFor.setAccessible(true);
} catch (Throwable error) {
getIdleForError = true;
cc.getIdleForError = true;
}
}
if (getIdleFor == null) {
if (cc.getIdleFor == null) {
return -1;
}
try {
Object connImpl = conn.unwrap(class_connectionImpl);
Object connImpl = conn.unwrap(cc.class_connectionImpl);
if (connImpl == null) {
return -1;
}
return System.currentTimeMillis()
- ((Long)
getIdleFor.invoke(connImpl))
.longValue();
return System.currentTimeMillis() - (Long) cc.getIdleFor.invoke(connImpl);
} catch (Exception e) {
throw new SQLException("getIdleFor error", e);
}
} else {
if (method_getIO == null && !method_getIO_error) {
if (cc.method_getIO == null && !cc.method_getIO_error) {
try {
method_getIO = class_connectionImpl.getMethod("getIO");
cc.method_getIO = cc.class_connectionImpl.getMethod("getIO");
} catch (Throwable error) {
method_getIO_error = true;
cc.method_getIO_error = true;
}
}
if (method_getIO == null) {
if (cc.method_getIO == null) {
return -1;
}
if (class_MysqlIO == null && !class_MysqlIO_Error) {
if (cc.class_MysqlIO == null && !cc.class_MysqlIO_Error) {
try {
class_MysqlIO = Utils.loadClass("com.mysql.jdbc.MysqlIO");
cc.class_MysqlIO = Utils.loadClass("com.mysql.jdbc.MysqlIO");
} catch (Throwable error) {
class_MysqlIO_Error = true;
cc.class_MysqlIO_Error = true;
}
}
if (class_MysqlIO == null) {
if (cc.class_MysqlIO == null) {
return -1;
}
if (method_getLastPacketReceivedTimeMs == null && !method_getLastPacketReceivedTimeMs_error) {
if (cc.method_getLastPacketReceivedTimeMs == null && !cc.method_getLastPacketReceivedTimeMs_error) {
try {
Method method = class_MysqlIO.getDeclaredMethod("getLastPacketReceivedTimeMs");
Method method = cc.class_MysqlIO.getDeclaredMethod("getLastPacketReceivedTimeMs");
method.setAccessible(true);
method_getLastPacketReceivedTimeMs = method;
cc.method_getLastPacketReceivedTimeMs = method;
} catch (Throwable error) {
method_getLastPacketReceivedTimeMs_error = true;
cc.method_getLastPacketReceivedTimeMs_error = true;
}
}
if (method_getLastPacketReceivedTimeMs == null) {
if (cc.method_getLastPacketReceivedTimeMs == null) {
return -1;
}
try {
Object connImpl = conn.unwrap(class_connectionImpl);
Object connImpl = conn.unwrap(cc.class_connectionImpl);
if (connImpl == null) {
return -1;
}
Object mysqlio = method_getIO.invoke(connImpl);
return (Long) method_getLastPacketReceivedTimeMs.invoke(mysqlio);
Object mysqlio = cc.method_getIO.invoke(connImpl);
return (Long) cc.method_getLastPacketReceivedTimeMs.invoke(mysqlio);
} catch (Exception e) {
throw new SQLException("getLastPacketReceivedTimeMs error", e);
}
}
}
static final Map<ClassLoader, ConnectionClass> classLoaderConnectionClassMap = new ConcurrentHashMap<>();
static class ConnectionClass {
private transient Class class_connectionImpl = null;
private transient boolean class_connectionImpl_Error = false;
private transient Method method_getIO = null;
private transient boolean method_getIO_error = false;
private transient Class class_MysqlIO = null;
private transient boolean class_MysqlIO_Error = false;
private transient Method method_getLastPacketReceivedTimeMs = null;
private transient boolean method_getLastPacketReceivedTimeMs_error = false;
private volatile boolean mysqlJdbcVersion6 = false;
private transient Class classJdbc = null;
private transient Method getIdleFor = null;
private transient boolean getIdleForError = false;
}
static Class<?> class_5_CommunicationsException = null;
static Class<?> class_6_CommunicationsException = null;

14
fine-druid/src/main/java/com/fr/third/alibaba/druid/util/Utils.java

@ -233,28 +233,26 @@ public class Utils {
}
public static Class<?> loadClass(String className) {
Class<?> clazz = null;
if (className == null) {
return null;
}
ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader();
if (ctxClassLoader != null) {
try {
return Class.forName(className);
return ctxClassLoader.loadClass(className);
} catch (ClassNotFoundException e) {
// skip
}
}
ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader();
if (ctxClassLoader != null) {
try {
clazz = ctxClassLoader.loadClass(className);
return Class.forName(className);
} catch (ClassNotFoundException e) {
// skip
}
}
return clazz;
return null;
}
private static Date startTime;

Loading…
Cancel
Save