From 0b5aeb4c95a79fe43bb5a73d784744450f90bb26 Mon Sep 17 00:00:00 2001 From: wangbin Date: Sat, 14 May 2022 15:58:41 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E6=95=B0=E6=8D=AE=E6=BA=90=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../datasource/service/impl/JdbcServiceImpl.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasource/service/impl/JdbcServiceImpl.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasource/service/impl/JdbcServiceImpl.java index 694aa2fd..016bad3f 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasource/service/impl/JdbcServiceImpl.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasource/service/impl/JdbcServiceImpl.java @@ -26,18 +26,21 @@ public class JdbcServiceImpl implements JdbcService { /** * 所有数据源的连接池存在map里 */ - static Map map = new ConcurrentHashMap<>(); + private Map 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 {