diff --git a/fine-druid/readme.MD b/fine-druid/readme.MD index 728b4f75d..fcaf361d7 100644 --- a/fine-druid/readme.MD +++ b/fine-druid/readme.MD @@ -1,6 +1,6 @@ # Alibaba Druid -- FineReport更新时间 `2022-04-27` +- FineReport更新时间 `2024-09-10` - Druid版本 1.2.9 - [github地址](https://github.com/alibaba/druid) @@ -8,12 +8,13 @@ > 由于需要适配数据源的一些特性和默认配置,对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配置属性的支持 | -| 1.2.9 | 2022-05-10 | |恢复DruidDataSource中的创建线程启停机制 | -| 1.2.9 | 2022-06-27 | REPORT-74418 |DruidConnectionHolder初始化时捕获AbstractMethodError异常防止获取getHoldability错误导致创建线程退出 | \ No newline at end of file +| 版本 | 日期 | 任务编号 | 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配置属性的支持 | +| 1.2.9 | 2022-05-10 | | 恢复DruidDataSource中的创建线程启停机制 | +| 1.2.9 | 2022-06-27 | REPORT-74418 | DruidConnectionHolder初始化时捕获AbstractMethodError异常防止获取getHoldability错误导致创建线程退出 | +| 1.2.9 | 2024-09-10 | KERNEL-18022 | 增加keepAlive系参数支持,增加部分带hibernate前缀的参数解析,以支持通过db.properties文件设置 | \ No newline at end of file diff --git a/fine-druid/src/main/java/com/fr/third/alibaba/druid/pool/DruidDataSourceFactory.java b/fine-druid/src/main/java/com/fr/third/alibaba/druid/pool/DruidDataSourceFactory.java index b3867c9bf..5b2ae74b4 100644 --- a/fine-druid/src/main/java/com/fr/third/alibaba/druid/pool/DruidDataSourceFactory.java +++ b/fine-druid/src/main/java/com/fr/third/alibaba/druid/pool/DruidDataSourceFactory.java @@ -15,13 +15,8 @@ */ package com.fr.third.alibaba.druid.pool; -import java.sql.Connection; -import java.sql.SQLException; -import java.util.Collections; -import java.util.Hashtable; -import java.util.Map; -import java.util.Properties; -import java.util.StringTokenizer; +import com.fr.third.alibaba.druid.support.logging.Log; +import com.fr.third.alibaba.druid.support.logging.LogFactory; import javax.naming.Context; import javax.naming.Name; @@ -29,9 +24,13 @@ import javax.naming.RefAddr; import javax.naming.Reference; import javax.naming.spi.ObjectFactory; import javax.sql.DataSource; - -import com.fr.third.alibaba.druid.support.logging.Log; -import com.fr.third.alibaba.druid.support.logging.LogFactory; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.Collections; +import java.util.Hashtable; +import java.util.Map; +import java.util.Properties; +import java.util.StringTokenizer; public class DruidDataSourceFactory implements ObjectFactory { @@ -58,6 +57,10 @@ public class DruidDataSourceFactory implements ObjectFactory { 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_KEEP_ALIVE = "keepAlive"; + public final static String HB_PROP_KEEP_ALIVE = HIBERNATE_PREFIX + "keepAlive"; + public final static String PROP_KEEP_ALIVE_BETWEEN_TIME_MILLIS = "keepAliveBetweenTimeMillis"; + public final static String HB_PROP_KEEP_ALIVE_BETWEEN_TIME_MILLIS = HIBERNATE_PREFIX + "keepAliveBetweenTimeMillis"; public final static String PROP_TESTONBORROW = "testOnBorrow"; public final static String HB_PROP_TESTONBORROW = HIBERNATE_PREFIX + "testOnBorrow"; public final static String PROP_TESTONRETURN = "testOnReturn"; @@ -80,11 +83,15 @@ public class DruidDataSourceFactory implements ObjectFactory { 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 HB_PROP_VALIDATIONQUERY_TIMEOUT = HIBERNATE_PREFIX + "validationQueryTimeout"; public final static String PROP_INITCONNECTIONSQLS = "initConnectionSqls"; public final static String PROP_ACCESSTOUNDERLYINGCONNECTIONALLOWED = "accessToUnderlyingConnectionAllowed"; public final static String PROP_REMOVEABANDONED = "removeAbandoned"; + public final static String HB_PROP_REMOVEABANDONED = HIBERNATE_PREFIX + "removeAbandoned"; public final static String PROP_REMOVEABANDONEDTIMEOUT = "removeAbandonedTimeout"; + public final static String HB_PROP_REMOVEABANDONEDTIMEOUT = HIBERNATE_PREFIX + "removeAbandonedTimeout"; public final static String PROP_LOGABANDONED = "logAbandoned"; + public final static String HB_PROP_LOGABANDONED = HIBERNATE_PREFIX + "logAbandoned"; public final static String PROP_POOLPREPAREDSTATEMENTS = "poolPreparedStatements"; public final static String PROP_MAXOPENPREPAREDSTATEMENTS = "maxOpenPreparedStatements"; public final static String PROP_CONNECTIONPROPERTIES = "connectionProperties"; @@ -105,6 +112,8 @@ public class DruidDataSourceFactory implements ObjectFactory { PROP_MINIDLE, PROP_INITIALSIZE, PROP_MAXWAIT, + PROP_KEEP_ALIVE, + PROP_KEEP_ALIVE_BETWEEN_TIME_MILLIS, PROP_TESTONBORROW, PROP_TESTONRETURN, PROP_TIMEBETWEENEVICTIONRUNSMILLIS, @@ -324,6 +333,36 @@ public class DruidDataSourceFactory implements ObjectFactory { } } + value = (String) properties.get(PROP_KEEP_ALIVE); + if (value != null) { + dataSource.setKeepAlive(Boolean.parseBoolean(value)); + } else { + Object o = properties.get(HB_PROP_KEEP_ALIVE); + if (o != null) { + if (o instanceof Boolean) { + dataSource.setKeepAlive((Boolean) o); + } else { + value = (String) o; + dataSource.setKeepAlive(Boolean.parseBoolean(value)); + } + } + } + + value = (String) properties.get(PROP_KEEP_ALIVE_BETWEEN_TIME_MILLIS); + if (value != null) { + dataSource.setKeepAliveBetweenTimeMillis(Long.parseLong(value)); + } else { + Object o = properties.get(HB_PROP_KEEP_ALIVE_BETWEEN_TIME_MILLIS); + if (o != null) { + if (o instanceof Integer) { + dataSource.setKeepAliveBetweenTimeMillis((int) o); + } else { + value = (String) o; + dataSource.setKeepAliveBetweenTimeMillis(Integer.parseInt(value)); + } + } + } + value = (String) properties.get(PROP_TESTONBORROW); if (value != null) { dataSource.setTestOnBorrow(Boolean.valueOf(value).booleanValue()); @@ -462,6 +501,11 @@ public class DruidDataSourceFactory implements ObjectFactory { value = (String) properties.get(PROP_VALIDATIONQUERY_TIMEOUT); if (value != null) { dataSource.setValidationQueryTimeout(Integer.parseInt(value)); + } else { + value = (String) properties.get(HB_PROP_VALIDATIONQUERY_TIMEOUT); + if (value != null && value.length() > 0) { + dataSource.setValidationQueryTimeout(Integer.parseInt(value)); + } } value = (String) properties.get(PROP_ACCESSTOUNDERLYINGCONNECTIONALLOWED); @@ -472,16 +516,31 @@ public class DruidDataSourceFactory implements ObjectFactory { value = (String) properties.get(PROP_REMOVEABANDONED); if (value != null) { dataSource.setRemoveAbandoned(Boolean.valueOf(value).booleanValue()); + } else { + value = (String) properties.get(HB_PROP_REMOVEABANDONED); + if (value != null && value.length() > 0) { + dataSource.setRemoveAbandoned(Boolean.parseBoolean(value)); + } } value = (String) properties.get(PROP_REMOVEABANDONEDTIMEOUT); if (value != null) { dataSource.setRemoveAbandonedTimeout(Integer.parseInt(value)); + } else { + value = (String) properties.get(HB_PROP_REMOVEABANDONEDTIMEOUT); + if (value != null && value.length() > 0) { + dataSource.setRemoveAbandonedTimeout(Integer.parseInt(value)); + } } value = (String) properties.get(PROP_LOGABANDONED); if (value != null) { dataSource.setLogAbandoned(Boolean.valueOf(value).booleanValue()); + } else { + value = (String) properties.get(HB_PROP_LOGABANDONED); + if (value != null && value.length() > 0) { + dataSource.setLogAbandoned(Boolean.parseBoolean(value)); + } } value = (String) properties.get(PROP_POOLPREPAREDSTATEMENTS); diff --git a/fine-druid/src/test/java/com/fr/third/alibaba/druid/pool/DruidDataSourceFactoryTest.java b/fine-druid/src/test/java/com/fr/third/alibaba/druid/pool/DruidDataSourceFactoryTest.java new file mode 100644 index 000000000..b7897daa7 --- /dev/null +++ b/fine-druid/src/test/java/com/fr/third/alibaba/druid/pool/DruidDataSourceFactoryTest.java @@ -0,0 +1,57 @@ +package com.fr.third.alibaba.druid.pool; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Properties; + +/** + * @author gaattc + * @since 6.1 + * Created by gaattc on 2024/9/10 + */ +public class DruidDataSourceFactoryTest { + + @Test + public void testConfigFromProperty() throws Exception { + try (DruidDataSource druidDataSource = new DruidDataSource()) { + Properties properties = new Properties(); + properties.setProperty("keepAlive", "true"); + properties.setProperty("keepAliveBetweenTimeMillis", "233333"); + properties.setProperty("validationQueryTimeout", "2333"); + properties.setProperty("removeAbandoned", "true"); + properties.setProperty("logAbandoned", "true"); + properties.setProperty("removeAbandonedTimeout", "2333"); + + DruidDataSourceFactory.config(druidDataSource, properties); + Assert.assertTrue(druidDataSource.isKeepAlive()); + Assert.assertEquals(233333, druidDataSource.getKeepAliveBetweenTimeMillis()); + Assert.assertEquals(2333, druidDataSource.getValidationQueryTimeout()); + Assert.assertTrue(druidDataSource.isRemoveAbandoned()); + Assert.assertTrue(druidDataSource.isLogAbandoned()); + Assert.assertEquals(2333, druidDataSource.getRemoveAbandonedTimeout()); + } + } + + @Test + public void testConfigFromPropertyWithPrefix() throws Exception { + try (DruidDataSource druidDataSource = new DruidDataSource()) { + Properties properties = new Properties(); + properties.setProperty("hibernate.keepAlive", "true"); + properties.setProperty("hibernate.keepAliveBetweenTimeMillis", "233333"); + properties.setProperty("hibernate.validationQueryTimeout", "2333"); + properties.setProperty("hibernate.removeAbandoned", "true"); + properties.setProperty("hibernate.logAbandoned", "true"); + properties.setProperty("hibernate.removeAbandonedTimeout", "2333"); + + DruidDataSourceFactory.config(druidDataSource, properties); + Assert.assertTrue(druidDataSource.isKeepAlive()); + Assert.assertEquals(233333, druidDataSource.getKeepAliveBetweenTimeMillis()); + Assert.assertEquals(2333, druidDataSource.getValidationQueryTimeout()); + Assert.assertTrue(druidDataSource.isRemoveAbandoned()); + Assert.assertTrue(druidDataSource.isLogAbandoned()); + Assert.assertEquals(2333, druidDataSource.getRemoveAbandonedTimeout()); + } + } + +} \ No newline at end of file