【7.2.5】【db】修复多数据源初始化过程中的参数设置问题

pull/37/head
fengshuonan 2022-09-15 11:08:04 +08:00
parent 4959f658cc
commit 02d78c9853
1 changed files with 104 additions and 0 deletions

View File

@ -24,6 +24,7 @@
*/
package cn.stylefeng.roses.kernel.dsctn.listener;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.db.api.factory.DruidDatasourceFactory;
@ -79,6 +80,9 @@ public class DataSourceInitListener extends ContextInitializedListener implement
druidProperties.setUsername(dataSourceUsername);
druidProperties.setPassword(dataSourcePassword);
// 设置其他数据源配置
this.setOtherDruidConfigs(environment, druidProperties);
// 创建主数据源
DruidDataSource druidDataSource = DruidDatasourceFactory.createDruidDataSource(druidProperties);
@ -93,4 +97,104 @@ public class DataSourceInitListener extends ContextInitializedListener implement
}
/**
* druid
*
* @author fengshuonan
* @date 2022/9/15 11:00
*/
private void setOtherDruidConfigs(ConfigurableEnvironment environment, DruidProperties druidProperties) {
// 连接池的相关配置
String initialSize = environment.getProperty("spring.datasource.initialSize");
String maxActive = environment.getProperty("spring.datasource.maxActive");
String minIdle = environment.getProperty("spring.datasource.minIdle");
String maxWait = environment.getProperty("spring.datasource.maxWait");
String poolPreparedStatements = environment.getProperty("spring.datasource.poolPreparedStatements");
String maxPoolPreparedStatementPerConnectionSize = environment.getProperty("spring.datasource.maxPoolPreparedStatementPerConnectionSize");
String validationQuery = environment.getProperty("spring.datasource.validationQuery");
String validationQueryTimeout = environment.getProperty("spring.datasource.validationQueryTimeout");
String testOnBorrow = environment.getProperty("spring.datasource.testOnBorrow");
String testOnReturn = environment.getProperty("spring.datasource.testOnReturn");
String testWhileIdle = environment.getProperty("spring.datasource.testWhileIdle");
String keepAlive = environment.getProperty("spring.datasource.keepAlive");
String timeBetweenEvictionRunsMillis = environment.getProperty("spring.datasource.timeBetweenEvictionRunsMillis");
String minEvictableIdleTimeMillis = environment.getProperty("spring.datasource.minEvictableIdleTimeMillis");
String filters = environment.getProperty("spring.datasource.filters");
// 设置配置
if (ObjectUtil.isNotEmpty(initialSize)) {
Integer intValue = Convert.toInt(initialSize);
druidProperties.setInitialSize(intValue);
}
if (ObjectUtil.isNotEmpty(maxActive)) {
Integer intValue = Convert.toInt(maxActive);
druidProperties.setMaxActive(intValue);
}
if (ObjectUtil.isNotEmpty(minIdle)) {
Integer intValue = Convert.toInt(minIdle);
druidProperties.setMinIdle(intValue);
}
if (ObjectUtil.isNotEmpty(maxWait)) {
Integer intValue = Convert.toInt(maxWait);
druidProperties.setMaxWait(intValue);
}
if (ObjectUtil.isNotEmpty(poolPreparedStatements)) {
boolean booleanValue = Convert.toBool(poolPreparedStatements);
druidProperties.setPoolPreparedStatements(booleanValue);
}
if (ObjectUtil.isNotEmpty(maxPoolPreparedStatementPerConnectionSize)) {
Integer intValue = Convert.toInt(maxPoolPreparedStatementPerConnectionSize);
druidProperties.setMaxPoolPreparedStatementPerConnectionSize(intValue);
}
if (ObjectUtil.isNotEmpty(validationQuery)) {
druidProperties.setValidationQuery(validationQuery);
}
if (ObjectUtil.isNotEmpty(validationQueryTimeout)) {
Integer intValue = Convert.toInt(validationQueryTimeout);
druidProperties.setValidationQueryTimeout(intValue);
}
if (ObjectUtil.isNotEmpty(testOnBorrow)) {
boolean booleanValue = Convert.toBool(testOnBorrow);
druidProperties.setTestOnBorrow(booleanValue);
}
if (ObjectUtil.isNotEmpty(testOnReturn)) {
boolean booleanValue = Convert.toBool(testOnReturn);
druidProperties.setTestOnReturn(booleanValue);
}
if (ObjectUtil.isNotEmpty(testWhileIdle)) {
boolean booleanValue = Convert.toBool(testWhileIdle);
druidProperties.setTestWhileIdle(booleanValue);
}
if (ObjectUtil.isNotEmpty(keepAlive)) {
boolean booleanValue = Convert.toBool(keepAlive);
druidProperties.setKeepAlive(booleanValue);
}
if (ObjectUtil.isNotEmpty(timeBetweenEvictionRunsMillis)) {
Integer intValue = Convert.toInt(timeBetweenEvictionRunsMillis);
druidProperties.setTimeBetweenEvictionRunsMillis(intValue);
}
if (ObjectUtil.isNotEmpty(minEvictableIdleTimeMillis)) {
Integer intValue = Convert.toInt(minEvictableIdleTimeMillis);
druidProperties.setMinEvictableIdleTimeMillis(intValue);
}
if (ObjectUtil.isNotEmpty(filters)) {
druidProperties.setFilters(filters);
}
}
}