Link to home
Start Free TrialLog in
Avatar of salilsharma72
salilsharma72

asked on

Tomcat and java.sql.SQLException: Connection is closed Exception.

I am using Apache-Tomcat integration using mod_jk2 and using common DBCP for the database connection pool. I am running a java application on the Tomcat server. Apache server just routes request to Tomcat. All application code runs on Tomcat server. It runs fine for couple of day but after that my application stops responding to any requests. This means that when I try to access servlet on my application, it takes forever to process the request which never comes back. I see following errors in my custom logs

ConnectionBean.releaseConnection Error: java.sql.SQLException: Connection is closed.

My data source properties for commons DB CP are
JdBC Driver Class = oracle.jdbc.driver.OracleDriver
Maximum Active Connection = 20
Maximum Idle connection = 10
Max wait for Connection = -1

I have made sure than I am closing a connection after opening it. Could you please advise what could be wrong? Is it a configuration issue or coding issue?
Please help.
Avatar of Tommy Braas
Tommy Braas
Flag of Australia image

Looks like either a configuration issue in Commons DBCP or a bug in same. Which version of Commons DBCP are you using?
Hi,

Assuming that you have restarted your Tomcat, will this behavior happen instantly or only after several times (days, hours or etc)? If it directly happens, it is probably your connection (configuration). If not, it may be your codes.

Regards
David
Avatar of salilsharma72
salilsharma72

ASKER

>>>Which version of Commons DBCP are you using?
I am using Tomcat 5.0 and it is using DBCP which comes with the Tomcat 5.0.

>>> Assuming that you have restarted your Tomcat, will this behavior happen instantly or only after several times (days, hours or etc)?
It does not happen right away, It runs file for couple of days and then stop responding. I think, it was just waiting for DB connection because the max Wait for Connection was set to -1 (means wait for indefinitely)

You can refer to DBCP properties meaning at http://jakarta.apache.org/commons/dbcp/configuration.html

After changing the commons DBCP configuration property Max. Wait for Connection = 90,000 milliseconds (it was -1 originally, means wait indefinitely), I am getting following error. It waits for 90 seconds and then gets another connection after throwing following exception. It looks like connection pool don't have any connection but it gets another connection from somewhere else. Do you guys know why this error is coming?

Thanks for the help.


Error: org.apache.commons.dbcp.SQLNestedException: Cannot get a connection, pool exhausted
org.apache.commons.dbcp.SQLNestedException: Cannot get a connection, pool exhausted
      at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:103)
      at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:540)
      at com.vision.interfaces.tools.ConnectionBean.getConnection(ConnectionBean.java:332)
      at com.vision.interfaces.tools.AWSSBeanBase.getNewTransID(AWSSBeanBase.java:132)
      at com.vision.interfaces.awsssync.rscBean.makeTrans(rscBean.java:410)
      at com.vision.interfaces.awsssync.rscBean.process(rscBean.java:101)
      at com.vision.interfaces.awsssync.syncProcess.callPro(syncProcess.java:112)
      at com.vision.interfaces.awsssync.syncProcess.run(syncProcess.java:49)
      at com.vision.interfaces.awsssync.SyncRscTask.run(SyncRscTask.java:26)
      at SyncReceiveServlet.processFile(SyncReceiveServlet.java:153)
      at SyncReceiveServlet.processRequest(SyncReceiveServlet.java:46)
      at SyncReceiveServlet.doGet(SyncReceiveServlet.java:55)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
      at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
      at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:300)
      at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:374)
      at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:743)
      at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:675)
      at org.apache.jk.common.SocketConnection.runIt(ChannelSocket.java:866)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
      at java.lang.Thread.run(Unknown Source)
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
      at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:756)
      at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:95)
      ... 39 more
Hi,

I am not pretty sure but do you have any codes that do not close the connection? Can you post your codes on how do you initialize and finalize the connection from DB?

Additionally, please have a look at the Preventing DB Connection Pools Leak part in
http://jakarta.apache.org/tomcat/tomcat-5.5-doc/jndi-datasource-examples-howto.html

I hope it worked.

David
I found the solution; my code was not closing a connection at one point.  Setting logAbandoned="true" in DBCP properties is really helpful as it will tell you the exact line of code where connection was opened but never closed. This kind of feature is not available in many application servers but it is available in Tomcat. I was pleasantly surprised.

Thanks everyone for the help

 
   

ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial