fix 数据源初始化优化

wangbin 3 years ago
parent dafae75bb4
commit 0b5aeb4c95

@ -26,18 +26,21 @@ public class JdbcServiceImpl implements JdbcService {
/**
* map
*/
static Map<Long, DruidDataSource> map = new ConcurrentHashMap<>();
private Map<Long, DruidDataSource> map = new ConcurrentHashMap<>();
private Object lock = new Object();
public DruidDataSource getJdbcConnectionPool(DataSourceDto dataSource) {
if (map.containsKey(dataSource.getId())) {
return map.get(dataSource.getId());
} else {
try {
if (!map.containsKey(dataSource.getId())) {
DruidDataSource pool = druidProperties.dataSource(dataSource.getJdbcUrl(),
dataSource.getUsername(), dataSource.getPassword(), dataSource.getDriverName());
map.put(dataSource.getId(), pool);
log.info("创建连接池成功:{}", dataSource.getJdbcUrl());
synchronized (lock) {
if (!map.containsKey(dataSource.getId())) {
DruidDataSource pool = druidProperties.dataSource(dataSource.getJdbcUrl(),
dataSource.getUsername(), dataSource.getPassword(), dataSource.getDriverName());
map.put(dataSource.getId(), pool);
log.info("创建连接池成功:{}", dataSource.getJdbcUrl());
}
}
return map.get(dataSource.getId());
} finally {

Loading…
Cancel
Save