我们正在运行一个带有 oracle 数据库的 websphere 商务站点,并面临数据库连接不足的问题。我们使用 JDBCHelper 单例来获取准备好的语句和连接连接。
public static JDBCHelper getJDBCHelper() {
if (theObject == null){
theObject = new JDBCHelper();
}
return theObject;
}
public void closeResources(Connection con, PreparedStatement pstmt, ResultSet rs){
try{
if(rs!=null){ rs.close();}
}catch(SQLException e){
logger.info("Exception closing the resultset");
}try{
if(pstmt!=null) { pstmt.close(); }
}catch(SQLException e){
logger.info("Exception closing the preparedstatement");
}try{
if(con!=null){ con.close(); }
}catch(SQLException e){
logger.info("Exception closing the connection");
}
}
但是,当我们尝试使用 prepStmt.getConnection() 获取连接以在执行后传递给关闭的资源时,它会引发 sql 异常。知道为什么吗?执行后连接是否立即关闭?我们使用单例 JDBCHelper 有什么问题吗?
编辑
生成准备好的语句、执行和关闭连接的部分代码
PreparedStatement pstmt = jdbcHelper.getPreparedStatement(query);
try{
//rest of the code
int brs = pstmt.executeUpdate();
}
finally{
try {
jdbcHelper.closeResources(pstmt.getConnection(),pstmt);
} catch (SQLException e1) {
logger.logp(Level.SEVERE,CLASS_NAME,methodName,"In the finally block - Could not close connection", e1);
}
}