我实现了 java web 服务,在其中,我有 DbUtil 类,我在线程中使用它来连接到 Oracle 数据库。问题是当我在 Tomcat 上部署 Web 服务时,它给了我关闭连接错误。
1106] ERROR xac.atws.service - SQL exception, while inserting request to DB. EX :
java.sql.SQLRecoverableException: Closed Connection
at oracle.jdbc.driver.PhysicalConnection.prepareCall(PhysicalConnection.java:3672)
at oracle.jdbc.driver.PhysicalConnection.prepareCall(PhysicalConnection.java:3633)
at xac.ape.at.ws.DbUtil.insertListMwRequestToDb(DbUtil.java:410)
at xac.ape.at.ws.jaxb.list.AtListProcessor.run(AtListProcessor.java:34)
at java.lang.Thread.run(Thread.java:745)
为了简要解释我在 DbUtil 类中所做的事情,我有公共类成员 conn、dbUser、dbPass、connectionUrl,它们由类构造函数填充。然后我使用这些变量在类的每个成员函数中打开和关闭一个新连接。dbUtil 类的实现图像附在链接中。DbUtil 类。
在每个成员函数中打开和关闭连接,如下所示: public DbUtil(String connectionUrlTemp, String dbUserTemp, String dbPassTemp) throws SQLException {
try {
//set the private connection variables
dbUser = dbUserTemp;
dbPass = dbPassTemp;
connectionUrl = connectionUrlTemp;
//initializing the connection
OracleDataSource ods = new OracleDataSource();
ods.setUser(dbUser);
ods.setPassword(dbPass);
ods.setURL(connectionUrl);
conn = ods.getConnection();
AtServiceImpl.loggerAtws.info("[SYSTEM] Successfully connected to DB");
AtServiceImpl.loggerAtws.debug("[SYSTEM] DB Connection URL : {}", connectionUrl);
} catch (SQLException e) {
AtServiceImpl.loggerAtws.error("[SYSTEM] Exception occured while initializing DB connection. EX : ", e);
}finally {
if (conn != null) {
conn.close();
}
}
}
public Hashtable<String, Integer> sysConfigPull(String systemName, String functionName) throws SQLException {
Hashtable<String, Integer> sysConfig = new Hashtable<>();
try {
//---- added this section ----
//initializing the connection
OracleDataSource ods = new OracleDataSource();
ods.setUser(dbUser);
ods.setPassword(dbPass);
ods.setURL(connectionUrl);
conn = ods.getConnection();
//---- end of added section ----
CallableStatement sqlStatement;
String SQLTEXT = "begin GETSYSCONFIG(?,?,?); end;";
sqlStatement = conn.prepareCall(SQLTEXT);
sqlStatement.setString(1, systemName);
sqlStatement.setString(2, functionName);
sqlStatement.registerOutParameter(3, OracleTypes.CURSOR);
ResultSet rs;
sqlStatement.executeUpdate();
rs = (ResultSet)sqlStatement.getObject(3);
while (rs.next()) {
sysConfig.put("RETRYCOUNT", rs.getInt("RETRYCOUNT"));
sysConfig.put("NOTICEID", rs.getInt("NOTICEID"));
AtServiceImpl.loggerAtws.debug("System Config option: RETRYCOUNT {}, NOTICEID {}",rs.getString("RETRYCOUNT"), rs.getString("NOTICEID"));
}
} catch (SQLException e) {
AtServiceImpl.loggerAtws.error("[SYSTEM] SQL Exception, load system configuration, EX:", e);
}finally {
if (conn != null) {
conn.close();
}
}
return sysConfig;
}
所以我认为在每个成员函数中打开和关闭连接可能会导致错误。如果是这样,为什么会这样?任何帮助,将不胜感激。