我实现了Java Web Service,并且在其中实现了DbUtil类,该类正在线程中用于连接到Oracle数据库。问题是,当我在Tomcat上部署Web服务时,它给了我Closed Connection错误。
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 class。
在每个成员函数中打开和关闭连接,如下所示: 公共DbUtil(String connectionUrlTemp,String dbUserTemp,String dbPassTemp)引发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;
}
所以我认为在每个成员函数中打开和关闭连接可能会导致错误。如果是这样,为什么会这样呢?任何帮助将不胜感激。