我的应用程序使用Oracle UCP监听少数表。在正式生产环境中,应用程序工作正常,但在测试环境中,应用程序从池中耗尽连接。我也在每个查询中关闭并返回使用过的连接。请参考连接关闭方法。
public static synchronized void closeConnection(Connection con) throws Exception {
String print;
try {
if (con != null && !con.isClosed()) {
con.close();
con =null;
Config.DB_POOL_COUNT_AVAILABLE = pds.getAvailableConnectionsCount();
Config.DB_POOL_COUNT_BUSSY = pds.getBorrowedConnectionsCount();
Config.DB_POOL_COUNT_OPENED = Config.DB_POOL_COUNT_AVAILABLE + Config.DB_POOL_COUNT_BUSSY;
} else {
Config.DB_POOL_COUNT_AVAILABLE = pds.getAvailableConnectionsCount();
Config.DB_POOL_COUNT_BUSSY = pds.getBorrowedConnectionsCount();
Config.DB_POOL_COUNT_OPENED = Config.DB_POOL_COUNT_AVAILABLE + Config.DB_POOL_COUNT_BUSSY;
}
} catch (Exception e) {
Logger.errorLog(e);
} finally {
Config.DB_POOL_COUNT_AVAILABLE = pds.getAvailableConnectionsCount();
Config.DB_POOL_COUNT_BUSSY = pds.getBorrowedConnectionsCount();
Config.DB_POOL_COUNT_OPENED = Config.DB_POOL_COUNT_AVAILABLE + Config.DB_POOL_COUNT_BUSSY;
if(con != null && !con.isClosed()){
con.close();
con=null;
}
}
}
因为你的应用程序在正式生产环境中运行良好,所以你不需要太关心连接池。在开发环境中,查询后关闭连接还不错。
有关Oracle UCP的良好实践,您可以在https://docs.oracle.com/cd/E18283_01/java.112/e12265/optimize.htm或
https://docs.oracle.com/en/database/oracle/oracle-database/18/jjucp/optimizing-ucp-behavior.html