qianlishi 2 years ago
commit bf1dc4a239

@ -26,19 +26,22 @@ public class JdbcServiceImpl implements JdbcService {
/** /**
* map * 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) { public DruidDataSource getJdbcConnectionPool(DataSourceDto dataSource) {
if (map.containsKey(dataSource.getId())) { if (map.containsKey(dataSource.getId())) {
return map.get(dataSource.getId()); return map.get(dataSource.getId());
} else { } else {
try { try {
synchronized (lock) {
if (!map.containsKey(dataSource.getId())) { if (!map.containsKey(dataSource.getId())) {
DruidDataSource pool = druidProperties.dataSource(dataSource.getJdbcUrl(), DruidDataSource pool = druidProperties.dataSource(dataSource.getJdbcUrl(),
dataSource.getUsername(), dataSource.getPassword(), dataSource.getDriverName()); dataSource.getUsername(), dataSource.getPassword(), dataSource.getDriverName());
map.put(dataSource.getId(), pool); map.put(dataSource.getId(), pool);
log.info("创建连接池成功:{}", dataSource.getJdbcUrl()); log.info("创建连接池成功:{}", dataSource.getJdbcUrl());
} }
}
return map.get(dataSource.getId()); return map.get(dataSource.getId());
} finally { } finally {
} }

Loading…
Cancel
Save