com.yuyenews.easy.init.InitDruidDataSource Maven / Gradle / Ivy
The newest version!
package com.yuyenews.easy.init;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yuyenews.easy.server.constant.EasySpace;
import com.yuyenews.easy.util.JdbcConfigUtil;
/**
* 数据源
* @author yuye
*
*/
public class InitDruidDataSource {
private static Logger logger = LoggerFactory.getLogger(InitDruidDataSource.class);
private static EasySpace commontSpace = EasySpace.getEasySpace();
/**
* 加载所有的数据源
*/
public static void loadDataSources() {
try {
JSONObject objs = JdbcConfigUtil.getConfig();
JSONArray dataSources = objs.getJSONArray("dataSource");
if(dataSources == null) {
return;
}
if(dataSources.size()>3) {
throw new Exception("数据源数量不可以超过3个");
}
Map map = new HashMap<>();
for(int i=0;i 0) {
dataSource.setInitialSize(initialSize);
}
int maxActive = jsonObject.getIntValue("maxActive");
if(maxActive > 0) {
dataSource.setMaxActive(maxActive);
}
int minIdle = jsonObject.getIntValue("minIdle");
if(minIdle > 0) {
dataSource.setMinIdle(minIdle);
}
long maxWait = jsonObject.getLongValue("maxWait");
if(maxWait > 0) {
dataSource.setMaxWait(maxWait);
}
int maxOpenPreparedStatements = jsonObject.getIntValue("maxOpenPreparedStatements");
if(maxOpenPreparedStatements > 0) {
dataSource.setMaxOpenPreparedStatements(maxOpenPreparedStatements);
}
Object validationQuery = jsonObject.get("validationQuery");
if(validationQuery != null) {
dataSource.setValidationQuery(validationQuery.toString());
}
long timeBetweenEvictionRunsMillis = jsonObject.getLongValue("timeBetweenEvictionRunsMillis");
if(timeBetweenEvictionRunsMillis > 0) {
dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
}
long minEvictableIdleTimeMillis = jsonObject.getLongValue("minEvictableIdleTimeMillis");
if(minEvictableIdleTimeMillis > 0) {
dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
}
Object connectionInitSqls = jsonObject.get("connectionInitSqls");
if(connectionInitSqls != null) {
JSONArray array = JSONArray.parseArray(connectionInitSqls.toString());
dataSource.setConnectionInitSqls(array);
}
Object exceptionSorter = jsonObject.get("exceptionSorter");
if(exceptionSorter != null) {
dataSource.setExceptionSorter(exceptionSorter.toString());
}
Object filters = jsonObject.get("filters");
if(filters != null) {
dataSource.setFilters(filters.toString());
}
int removeAbandonedTimeout = jsonObject.getIntValue("removeAbandonedTimeout");
if(removeAbandonedTimeout > 0) {
dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
}
boolean logAbandoned = jsonObject.getBooleanValue("logAbandoned");
dataSource.setLogAbandoned(logAbandoned);
boolean removeAbandoned = jsonObject.getBooleanValue("removeAbandoned");
dataSource.setRemoveAbandoned(removeAbandoned);
boolean testOnBorrow = jsonObject.getBooleanValue("testOnBorrow");
dataSource.setTestOnBorrow(testOnBorrow);
boolean testOnReturn = jsonObject.getBooleanValue("testOnReturn");
dataSource.setTestOnReturn(testOnReturn);
boolean testWhileIdle = jsonObject.getBooleanValue("testWhileIdle");
dataSource.setTestWhileIdle(testWhileIdle);
boolean poolPreparedStatements = jsonObject.getBooleanValue("poolPreparedStatements");
dataSource.setPoolPreparedStatements(poolPreparedStatements);
map.put(jsonObject.getString("name"), dataSource);
}
commontSpace.setAttr("dataSources", map);
} catch (Exception e) {
logger.error("创建数据源的时候报错",e);
}
}
}