title: Tomcat Connection Pool Connection has already been closed
body: <p>In our application, we have configured database connection pools using Tomcat DBCP. Our application has more than 150 databases. We can't set the max active connections for a pool more than 2. Because our server resources are limited. While performance test execution with 100 users, some requests failed by throwing the following error randomly. So I would like to know if there is a way that we can fine tune the pool configurations to resolve this issue.</p>
  
  <p>Data Source Configurations in Tomcat's server.xml</p>
  
  <pre><code>&lt;!-- Data Source --&gt;
  &lt;Resource name="jdbc/openedge/xxxxx/services/xxxx" auth="Container"
      factory="main.org.apache.tomcat.jdbc.pool.DataSourceFactory" type="javax.sql.DataSource"
      driverClassName="com.ddtek.jdbc.openedge.OpenEdgeDriver"
      url="jdbc:datadirect:openedge://xxxxx"
      username="xxxxxx" 
      password="xxxxxx" 
      initialSize="2" 
      maxActive="2"
      maxIdle="2" 
      minIdle="2" 
      removeAbandoned="true" 
      removeAbandonedTimeout="900" 
      logAbandoned="true"
      global="jdbc/openedge/xxxxx/services/xxxx" 
      maxWait="-1" 
      testOnBorrow="true"
      validationInterval="30000" 
      validatorClassName="main.java.com.xxxxxxxx.util.DBConnectionValidator" 
      validationTimeout="2"  /&gt;
  </code></pre>
  
  <p>Error Stack</p>
  
  <pre><code>&lt;SSS-MAT-61771&gt;  2014/Oct/01 10:07:32,599 WARN  [SqlExceptionHelper] SQL Error: 0, SQLState: null
  &lt;SSS-MAT-61771&gt;  2014/Oct/01 10:07:32,599 ERROR [SqlExceptionHelper] Connection has already been  closed.
  &lt;SSS-MAT-61771&gt;  2014/Oct/01 10:07:32,599 INFO  [BaseDaoImpl] closeSession starting:        BBBCompositeDaoImpl
  &lt;SSS-MAT-61771&gt;  2014/Oct/01 10:07:32,599 INFO  [BaseDaoImpl] closeSession completed: BBBCompositeDaoImpl
  &lt;SSS-MAT-61771&gt;  2014/Oct/01 10:07:32,599 ERROR [ExceptionHandler] Internal server error
  main.java.com.xxxxxxxxx.common.exception.DataServiceException: Internal server error
      at     main.java.com.xxxxxxxxx.xxxxxxxx.services.ManageAssetsRestImpl.getMethodInManageAssests(Unknown     Source)
      at sun.reflect.GeneratedMethodAccessor124.invoke(Unknown Source)
      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:180)
      at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96)
      at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:193)
      at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:102)
      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:271)
      at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
      at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:239)
      at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:218)
      at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:198)
      at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:137)
      at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:158)
      at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:243)
      at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:163)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
      at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:219)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at main.java.com.xxxxxxxxx.xxxxxxxx.filter.LogFilter.doFilter(Unknown Source)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1600)
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
      at java.lang.Thread.run(Thread.java:662)
  
  Caused by: org.hibernate.exception.GenericJDBCException: could not inspect JDBC autocommit mode
      at     org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
      at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.isAutoCommit(LogicalConnectionImpl.java:395)
      at org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl.afterNonTransactionalQuery(TransactionCoordinatorImpl.java:195)
      at org.hibernate.internal.SessionImpl.afterOperation(SessionImpl.java:598)
      at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1790)
      at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:226)
      at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:156)
      at main.java.com.xxxxxxxxx.common.dao.impl.BBBCompositeDaoImpl.getBBBIds(Unknown Source)
      at main.java.com.xxxxxxxxx.xxxxxxxx.business.delegates.ManageAssetsDelegate.getBBBIdentifiers(Unknown Source)
      at main.java.com.xxxxxxxxx.xxxxxxxx.business.delegates.ManageAssetsDelegate.getBBBIds(Unknown Source)
      at main.java.com.xxxxxxxxx.xxxxxxxx.services.ManageAssetsRestImpl.getBBBIds(Unknown Source)
      ... 40 more
  Caused by: java.sql.SQLException: Connection has already been closed.
      at org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:117)
      at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:109)
      at     org.apache.tomcat.jdbc.pool.DisposableConnectionFacade.invoke(DisposableConnectionFacade.java:80)
      at $Proxy166.getAutoCommit(Unknown Source)
      at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.isAutoCommit(LogicalConnectionImpl.java:392)
      ... 49 more
  </code></pre>
  
  <p>Hibernate Configurations for routing databases at runtime.</p>
  
  <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt;
   &lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:jee="http://www.springframework.org/schema/jee" xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/tx 
   http://www.springframework.org/schema/tx/spring-tx.xsd
   http://www.springframework.org/schema/jee
   http://www.springframework.org/schema/jee/spring-jee-3.0.xsd"
   default-lazy-init="true" default-autowire="byName"&gt;
  
  &lt;bean id="bxxHistoryDao"
      class="main.java.com.xxxxxxxx.common.dao.impl.bxxHistoryDaoImpl"
      scope="prototype"&gt;
      &lt;property name="sessionFactory" ref="bxxSessionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="bxxDataSource"
      class="main.java.com.xxxxxxxx.xxxxdataservices.openedge.BaseRoutingDataSource"&gt;
      &lt;property name="targetDataSources"&gt;
          &lt;map key-type="java.lang.String"&gt;
              &lt;entry key="AXXXX" value-ref="bxxDataSource_AXXXX" /&gt;
              &lt;entry key="EXXXX" value-ref="bxxDataSource_EXXXX" /&gt;
          &lt;/map&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  &lt;bean id="bxxDataSource_AXXXX"
      class="main.java.com.xxxxxxxx.xxxxdataservices.openedge.BxxRoutingDataSource"&gt;
      &lt;property name="targetDataSources"&gt;
          &lt;map key-type="java.lang.String"&gt;
              &lt;entry key="BXXX1" value-ref="BXXX1DataSource_AXXXX" /&gt;
              &lt;entry key="BXXX2" value-ref="BXXX2DataSource_AXXXX" /&gt;
          &lt;/map&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  &lt;bean id="bxxDataSource_EXXXX"
      class="main.java.com.xxxxxxxx.xxxxdataservices.openedge.BxxRoutingDataSource"&gt;
      &lt;property name="targetDataSources"&gt;
          &lt;map key-type="java.lang.String"&gt;
              &lt;entry key="BXXX1" value-ref="BXXX1DataSource_EXXXX" /&gt;
              &lt;entry key="BXXX2" value-ref="BXXX2DataSource_EXXXX" /&gt;
          &lt;/map&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="parentDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"
      abstract="true" /&gt;
  &lt;bean id="BXXX1DataSource_AXXXX" parent="parentDataSource"&gt;
      &lt;property name="jndiName"
          value="java:comp/env/jdbc/openedge/BXXX1/rmservices/AXXXX" /&gt;
  &lt;/bean&gt;
  &lt;bean id="BXXX2DataSource_AXXXX" parent="parentDataSource"&gt;
      &lt;property name="jndiName"
          value="java:comp/env/jdbc/openedge/BXXX2/rmservices/AXXXX" /&gt;
  &lt;/bean&gt;
  &lt;bean id="BXXX1DataSource_EXXXX" parent="parentDataSource"&gt;
      &lt;property name="jndiName"
          value="java:comp/env/jdbc/openedge/BXXX1/rmservices/EXXXX" /&gt;
  &lt;/bean&gt;
  &lt;bean id="BXXX2DataSource_EXXXX" parent="parentDataSource"&gt;
      &lt;property name="jndiName"
          value="java:comp/env/jdbc/openedge/BXXX2/rmservices/EXXXX" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="parentSessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
      abstract="true"&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.ProgressDialect&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;prop key="hibernate.cache.use_second_level_cache"&gt;false&lt;/prop&gt;
              &lt;prop key="hibernate.cache.region.factory_class"&gt;net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
              &lt;/prop&gt;
              &lt;prop key="hibernate.cache.use_query_cache"&gt;false&lt;/prop&gt;
              &lt;prop key="hibernate.connection.autocommit"&gt;false&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="bxxSessionFactory" parent="parentSessionFactory"&gt;
      &lt;property name="dataSource" ref="bxxDataSource" /&gt;
      &lt;property name="packagesToScan" value="main.java.com.xxxxxxxx.common.domain.box" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="bxxTransactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="bxxSessionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven transaction-manager="bxxTransactionManager" /&gt;
  </code></pre>
  
  <p></p>