20260822:数据库桥接资源泄露bug修复

This commit is contained in:
zhugongping 2026-08-22 18:29:14 +08:00
parent 725df3aa29
commit 1e15ae7400
2 changed files with 21 additions and 14 deletions

View File

@ -92,6 +92,7 @@ public class DataSourceService {
Bridge bridge = bridgeService.queryByIdWithCache(id); Bridge bridge = bridgeService.queryByIdWithCache(id);
if (bridge == null || bridge.getType() != 5) { if (bridge == null || bridge.getType() != 5) {
log.error("数据源不存在或者当前配置类型不是数据库存储,id={}", id); log.error("数据源不存在或者当前配置类型不是数据库存储,id={}", id);
return null;
} }
return JSON.parseObject(bridge.getConfigJson(), MultipleDataSource.class); return JSON.parseObject(bridge.getConfigJson(), MultipleDataSource.class);
} }

View File

@ -21,27 +21,33 @@ public class DatabaseBridge extends NodeComponent {
@Override @Override
public void process() throws Exception { public void process() throws Exception {
MsgContext cxt = this.getContextBean(MsgContext.class); MsgContext cxt = this.getContextBean(MsgContext.class);
Long id = cxt.getData("databaseBridgeID"); Integer id = cxt.getData("databaseBridgeID");
MultipleDataSource multipleDataSource = dataSourceService.getDataSource(id); if (id == null) {
log.error("databaseBridgeID未设置,跳过数据库桥接");
return;
}
MultipleDataSource multipleDataSource = dataSourceService.getDataSource(Long.valueOf(id));
if (multipleDataSource == null) {
log.error("multipleDataSource为空,请检查桥接配置");
return;
}
DruidDataSource dataSource = dataSourceService.createDataSource(multipleDataSource); DruidDataSource dataSource = dataSourceService.createDataSource(multipleDataSource);
DruidPooledConnection connection = null;
Statement statement = null;
String sql = cxt.placeholders(multipleDataSource.getSql()); String sql = cxt.placeholders(multipleDataSource.getSql());
log.info("执行sql:{}", sql); log.info("执行sql:{}", sql);
try { if ("SELECT 1;".equalsIgnoreCase(sql)) {
connection = dataSource.getConnection(); // 不要浪费资源直接返回
statement = connection.createStatement(); return;
statement.execute(sql);
} catch (SQLException e) {
log.error("sql执行失败---------,原因:{ }", e);
} }
try { try {
connection.close(); try (DruidPooledConnection connection = dataSource.getConnection();
if (statement != null) { Statement statement = connection.createStatement()) {
statement.close(); statement.execute(sql);
} }
} catch (SQLException e) { } catch (SQLException e) {
log.error("关闭数据库连接失败,原因:{ }", e); log.error("sql执行失败---------,原因:", e);
} finally {
dataSource.close();
} }
} }