我使用weblogic应用程序服务器和oracle数据库。我使用jdbc与oracle数据库进行通信。我从weblogic数据源获得连接,并将记录插入表中。问题是,当我想关闭连接(插入数据库后)时,我将遇到一个异常(连接已关闭)。这是我的代码:
private Connection getConnection() {
try {
Context context = new InitialContext();
DataSource datasource = (DataSource) context.lookup("jdbc/datasource-uat");
Connection connection = datasource.getConnection();
connection.setAutoCommit(false);
return connection;
} catch (Throwable throwable) {
throwable.printStackTrace();
return null;
}
}
public void persist(Long id) {
Connection connection = null;
try {
connection = getConnection();
PreparedStatement preparedStatement;
ResultSet resultSet = null;
if (id == 0L) {
synchronized (this) {
preparedStatement = connection.prepareStatement("SELECT MY_BEAN_SEQ.NEXTVAL FROM DUAL");
resultSet = preparedStatement.executeQuery();
}
resultSet.next();
id = resultSet.getLong("NEXTVAL");
}
preparedStatement = connection.prepareStatement("INSERT INTO MY_BEAN (ID) VALUES (?)");
preparedStatement.setLong(1, id);
preparedStatement.executeUpdate();
connection.commit();
if (resultSet != null) {
resultSet.close();
}
preparedStatement.close();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
finally {
closeConnection(connection);
}
}
private void closeConnection(Connection connection) {
if (connection != null)
try {
connection.close();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
但是connection.close语句会引发异常:
java.sql.SQLException: Connection has already been closed.
at weblogic.jdbc.wrapper.PoolConnection.checkConnection(PoolConnection.java:62)
at weblogic.jdbc.wrapper.Connection.preInvocationHandler(Connection.java:100)
at weblogic.jdbc.wrapper.Connection.getMetaData(Connection.java:476)
at com.DataAccess.closeConnection(DataAccess.java:61)
at com.DataAccess.persist(DataAccess.java:289)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:188)
at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:104)
at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:204)
at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:101)
at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)
at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:94)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:249)
at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:248)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:222)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:153)
at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:181)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:289)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:209)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:265)
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:301)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:184)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3732)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3696)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2273)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2179)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1490)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)
我试图避免使用connection.close语句(因为我教过连接会自动关闭!!但过了一会儿,所有的连接都被打开,并因此引发了异常)
答案 0 :(得分:0)
尝试使用try-with-resources Statement“自动”关闭连接。
类似于this
答案 1 :(得分:0)
查看PreparedStatement示例以供参考。
答案 2 :(得分:-1)
从程序中删除此行代码
if (resultSet != null) {
resultSet.close();
}
由于此行,close方法被调用了两次
resultSet.close(); //It close the connection here
preparedStatement.close(); //Then this line is give error because your connection is already closed
您可以删除preparedStatement.close();
或resultSet.close();