u
This commit is contained in:
1 parent
422d206bd6
commit
a4bd92264d
108 files changed
+16862
-35
No files matched your search
+40
-10
@@ -13,6 +13,7 @@ import java.util.Properties;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class DataSourceManager extends AbstractRoutingDataSource {
|
||||
|
||||
private static final String CONFIG_PATH = "dbconfigs/";
|
||||
private static final Map<String, DataSource> dataSources = new ConcurrentHashMap<>();
|
||||
|
||||
@@ -26,10 +27,16 @@ public class DataSourceManager extends AbstractRoutingDataSource {
|
||||
return DataSourceContextHolder.getDataSourceKey();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取连接(带异常保护)
|
||||
*/
|
||||
public static Connection getConnection(String orgCode) throws Exception {
|
||||
DataSource dataSource = getOrCreateDataSource(orgCode);
|
||||
|
||||
return dataSource.getConnection();
|
||||
try {
|
||||
DataSource dataSource = getOrCreateDataSource(orgCode);
|
||||
return dataSource.getConnection();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("获取数据库连接失败: " + orgCode, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,9 +56,21 @@ public class DataSourceManager extends AbstractRoutingDataSource {
|
||||
return ds;
|
||||
}
|
||||
|
||||
// 创建数据源
|
||||
ds = createDataSource(orgCode);
|
||||
dataSources.put(orgCode, ds);
|
||||
|
||||
// ===== 关键:先验证连接,防止缓存坏数据源 =====
|
||||
try (Connection conn = ds.getConnection()) {
|
||||
// 成功才缓存
|
||||
dataSources.put(orgCode, ds);
|
||||
} catch (Exception e) {
|
||||
if (ds instanceof DruidDataSource) {
|
||||
((DruidDataSource) ds).close();
|
||||
}
|
||||
throw new RuntimeException("数据库连接失败: " + orgCode, e);
|
||||
}
|
||||
|
||||
// 刷新 routing datasource
|
||||
refresh();
|
||||
|
||||
return ds;
|
||||
@@ -62,14 +81,13 @@ public class DataSourceManager extends AbstractRoutingDataSource {
|
||||
* 刷新 routing datasource
|
||||
*/
|
||||
private static void refresh() {
|
||||
|
||||
DataSourceManager routing = SpringUtils.getBean(DataSourceManager.class);
|
||||
routing.setTargetDataSources(new ConcurrentHashMap<>(dataSources));
|
||||
routing.afterPropertiesSet();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建数据源
|
||||
* 创建数据源(已优化:防止无限重连/卡死)
|
||||
*/
|
||||
private static DataSource createDataSource(String orgCode) throws Exception {
|
||||
|
||||
@@ -93,11 +111,23 @@ public class DataSourceManager extends AbstractRoutingDataSource {
|
||||
ds.setPassword(p.getProperty("password"));
|
||||
ds.setDriverClassName(p.getProperty("driver"));
|
||||
|
||||
ds.setInitialSize(2);
|
||||
ds.setMaxActive(20);
|
||||
ds.setMinIdle(2);
|
||||
ds.setValidationQuery("SELECT 1");
|
||||
// ===== 基础连接池配置 =====
|
||||
ds.setInitialSize(1);
|
||||
ds.setMaxActive(10);
|
||||
ds.setMinIdle(1);
|
||||
|
||||
// ===== 核心:防止卡死 / 无限重试 =====
|
||||
ds.setMaxWait(3000); // 获取连接最大等待3秒
|
||||
ds.setBreakAfterAcquireFailure(true); // 获取失败直接抛异常
|
||||
ds.setConnectionErrorRetryAttempts(0); // 不重试
|
||||
ds.setFailFast(true); // 初始化时检测连接
|
||||
|
||||
// ===== 连接校验 =====
|
||||
ds.setValidationQuery("SELECT 1");
|
||||
ds.setTestOnBorrow(true);
|
||||
ds.setTestWhileIdle(false);
|
||||
|
||||
// 初始化(此时若连接失败会快速抛异常)
|
||||
ds.init();
|
||||
|
||||
return ds;
|
||||
|
||||
@@ -22,8 +22,8 @@ file:
|
||||
base-dir: D:/data/file
|
||||
|
||||
static-path: /static/file
|
||||
server-url: http://192.168.1.137:80/api
|
||||
|
||||
# server-url: http://192.168.1.137:80/api
|
||||
server-url: http://ser369.g3soft.cn:80/api
|
||||
common-dir: common
|
||||
container-dir: container
|
||||
temp-dir: temp
|
||||
@@ -1,10 +1,10 @@
|
||||
url=jdbc:sqlserver://118.89.70.199:1433;databaseName=G3BoxOCR
|
||||
#url=jdbc:sqlserver://118.89.70.199:1433;databaseName=G3BoxOCR
|
||||
#username=sa
|
||||
#password=zhang520..
|
||||
#driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
|
||||
|
||||
url=jdbc:sqlserver://127.0.0.1:1433;databaseName=G3ContainerOCR
|
||||
username=sa
|
||||
password=zhang520..
|
||||
password=123456@aaa
|
||||
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
|
||||
|
||||
# url=jdbc:sqlserver://127.0.0.1:1433;databaseName=G3BoxOCR
|
||||
# username=sa
|
||||
# password=123456@aaa
|
||||
# driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
|
||||
|
||||
Reference in new issue
Block a user