I use weblogic application server and oracle database. I use jdbc for communicating with the oracle database. I get the connection from weblogic datasource and insert a record to the table. The problem is that when I want to close the connection (after inserting to the database) I'll face with an exception (Connection has already been closed) .This is my code:
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();
}
}
But connection.close statement throws an exception:
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)
I tried to avoid connection.close statement (because I taught that connection is closed automatically!! but after a while all of the connections were opened and an exception was thrown due to that)
if (connection != null && connection.isOpen())
(I don't remember the exact syntax) – Leviand