fix(resource): JDBCPoolInit 实现 AutoCloseable 添加 close() 方法

原代码单例模式无关闭方法,应用退出时数据库连接池无法释放。

改为:
- 实现 AutoCloseable 接口
- 添加 close() 方法关闭连接池
- 关闭后将 pool 置 null 防止重复关闭
This commit is contained in:
yukaidi
2026-05-29 00:32:26 +08:00
parent 8dfcf510f6
commit 6c60b0116f

View File

@@ -17,7 +17,7 @@ import org.slf4j.LoggerFactory;
* *
* @author <a href="https://qaiu.top">QAIU</a> * @author <a href="https://qaiu.top">QAIU</a>
*/ */
public class JDBCPoolInit { public class JDBCPoolInit implements AutoCloseable {
private static final Logger LOGGER = LoggerFactory.getLogger(JDBCPoolInit.class); private static final Logger LOGGER = LoggerFactory.getLogger(JDBCPoolInit.class);
@@ -101,4 +101,16 @@ public class JDBCPoolInit {
synchronized public JDBCPool getPool() { synchronized public JDBCPool getPool() {
return pool; return pool;
} }
/**
* 关闭连接池,释放数据库资源
*/
@Override
public synchronized void close() {
if (pool != null) {
pool.close();
LOGGER.info("数据库连接池已关闭: URL={}", url);
pool = null;
}
}
} }