我的配置是: 應用服務器:Weblogic的10gR3 數據庫:Oracle 10g的 IDE:Netbeans的 的Java:第7版值java.sql.SQLException:連接已經第一數據庫查詢後關閉
我面對異常同時從游泳池獲取連接。我曾嘗試使用資源來避免錯誤地關閉連接和語句的負擔。
以下是堆棧跟蹤。
[INFO] 2016-06-24 19:01:08,304 org.nmdc.incentivepackage.databaseFiles.DepositMDao checkData 184 - Executing query SELECT count(*) count FROM dep_m
[INFO] 2016-06-24 19:01:08,304 org.nmdc.incentivepackage.databaseFiles.DepositMDao checkData 188 - Executing query SELECT count(*) count FROM dep_m
[INFO] 2016-06-24 19:01:08,320 org.nmdc.incentivepackage.databaseFiles.DepositMDao checkData 194 - Relinquish Database connection ...
[INFO] 2016-06-24 19:01:08,320 org.nmdc.incentivepackage.databaseFiles.DepositMDao checkData 206 - return 1###success###
[INFO] 2016-06-24 19:01:08,320 org.nmdc.incentivepackage.controllerFiles.CheckDepositMDataController processRequest 35 - result 1###success### .........save
[INFO] 2016-06-24 19:01:08,320 org.nmdc.incentivepackage.controllerFiles.CheckDepositMDataController processRequest 46 - update deposit master table
[INFO] 2016-06-24 19:01:08,320 org.nmdc.incentivepackage.controllerFiles.GetDepositMDataController processRequest 33 - Fetching deposit master data
[ERROR] 2016-06-24 19:01:08,320 org.nmdc.incentivepackage.databaseFiles.DepositMDao getDepositMData 239 - Exception
java.sql.SQLException: Connection has already been closed.
at weblogic.jdbc.wrapper.PoolConnection.checkConnection(PoolConnection.java:55)
at weblogic.jdbc.wrapper.Connection.preInvocationHandler(Connection.java:92)
at weblogic.jdbc.wrapper.Connection.createStatement(Connection.java:441)
at org.nmdc.incentivepackage.databaseFiles.DepositMDao.getDepositMData(DepositMDao.java:219)
at org.nmdc.incentivepackage.controllerFiles.GetDepositMDataController.processRequest(GetDepositMDataController.java:35)
at org.nmdc.incentivepackage.controllerFiles.GetDepositMDataController.doPost(GetDepositMDataController.java:74)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3498)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(Unknown Source)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2180)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2086)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1406)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
的DBUtility類,使該連接對象是
public class DBUtility {
private static Connection conn;
private static Context ctx ;
final static Logger logger = LoggerFactory.getLogger(DBUtility.class);
public DBUtility() {
ctx = null;
conn = null;
}
public static Connection getConnection() throws Exception {
if (conn != null)
return conn;
else {
//Hashtable ht = new Hashtable();
// ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
// ht.put(Context.PROVIDER_URL,"t3://localhost:7001"); //weblogic server
try {
ctx = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup ("incentivepool");
conn = ds.getConnection();
}
catch(Exception e) {
logger.error("Error in establishing connection...",e);
}
}
return conn;
}
}
被拋出異常的類是
ublic class DepositMDao extends HttpServlet {
private Connection connection = null;
final static Logger log = LoggerFactory.getLogger(DepositMDao.class);
private String deposit,category,period,query;
private float amount;
private String errors,status;
private int errorCount;
private Statement statement;
private ResultSet rs;
JSONObject jobject;
public DepositMDao() throws Exception {
// connection = DBUtility.getConnection();
if(connection != null) {
log.info("Received Database connection ...");
}
errors ="";
status = "success";
errorCount =0;
statement = null;
}
public String checkData() throws SQLException {
int count=0;
query = "SELECT count(*) count FROM dep_m" ;
log.info("Executing query "+query);
try (Connection connection = DBUtility.getConnection();
Statement statement = connection.createStatement();
) {
log.info("Executing query "+query);
try (ResultSet rs = statement.executeQuery(query)) {
if(rs.next()) {
count = rs.getInt("count");
}
}
log.info("Relinquish Database connection ...");
} catch(Exception ex) {
errorCount++;
errors = errors + " "+ex.getMessage();
log.error("Exception ",ex);
}
if(errorCount == 0) {
status = "success";
} else {
status = "failure";
}
log.info("return "+count+"###"+status + "###"+errors);
return count+"###"+status + "###"+errors;
}
public List<DepositMBean> getDepositMData() throws SQLException {
List<DepositMBean> depositList = new ArrayList();
DepositMBean depositBean;
String dep14,dep11C,dep11B;
depositBean = null;
depositList = null;
query = "SELECT c_dep14,c_dep11C,c_dep11B FROM dep_m ";
try (Connection connection1 = DBUtility.getConnection();
Statement statement1 = connection1.createStatement();
)
{
log.info("Executing query "+query);
try (ResultSet rs1 = statement1.executeQuery(query)) {
while (rs1.next()) {
depositBean = new DepositMBean();
dep14 = rs1.getString("c_dep14");
dep11C = rs1.getString("c_dep11C");
dep11B = rs1.getString("c_dep11B");
depositBean.setcDep14(dep14);
depositBean.setcDep11B(dep11B);
depositBean.setcDep11C(dep11C);
depositList.add(depositBean);
}
depositBean.display();
log.info("Relinquish Database connection ...");
}
} catch(Exception ex) {
log.error("Exception ",ex);
}
return depositList;
}
在服務器池配置是:
不知道什麼是錯的,但你可能要查找/初始化數據源只有一次......我把它在一些初始化,而不是一個getter內方法。檢查旁邊是連接,而不是數據源 – Zeromus
不要將'Connection'存儲爲您的servlet或'DBUtility'類的字段。該Web連接被Web容器回收並在Web請求完成時返回到連接池。容器認爲它是一個懸掛/孤立連接和編碼錯誤,完全正確。 – Andreas