title: hibernate reestablish connection after being lost
body: <p>I work with hibernate and oracle ,</p>
  
  <p>I have this error when I didn't use my application for a period :</p>
  
  <pre><code>18:08:15,140 WARN  [JDBCExceptionReporter] SQL Error: 2396, SQLState: 61000
  18:08:15,140 ERROR [JDBCExceptionReporter] ORA-02396: exceeded maximum idle time, please connect again
  
  18:08:15,140 ERROR [STDERR] ------ Connection is closed
  18:08:15,343 ERROR [JDBCTransaction] JDBC commit failed
  java.sql.SQLException: ORA-02396: exceeded maximum idle time, please connect again
  
          at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113)
          at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
          at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:283)
          at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:278)
          at oracle.jdbc.driver.T4C7Ocommoncall.receive(T4C7Ocommoncall.java:141)
          at oracle.jdbc.driver.T4CConnection.doCommit(T4CConnection.java:539)
          at oracle.jdbc.driver.PhysicalConnection.commit(PhysicalConnection.java:1181)
          at org.apache.commons.dbcp.DelegatingConnection.commit(DelegatingConnection.java:301)
          at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.commit(PoolingDataSource.java:200)
          at org.hibernate.transaction.JDBCTransaction.commitAndResetAutoCommit(JDBCTransaction.java:170)
          at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:146)
          at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
          at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:752)
          at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:721)
          at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:394)
          at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:117)
          at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
          at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
          at $Proxy507.getInfoEmploye(Unknown Source)
          at com.dq.admincom.web.service.BalanceService.getInfoEmploye(BalanceService.java:32)
          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.axis.providers.java.RPCProvider.invokeMethod(RPCProvider.java:397)
          at org.apache.axis.providers.java.RPCProvider.processMessage(RPCProvider.java:186)
          at org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:323)
          at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
          at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
          at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
          at org.apache.axis.handlers.soap.SOAPService.invoke(SOAPService.java:454)
          at org.apache.axis.server.AxisServer.invoke(AxisServer.java:281)
          at org.apache.axis.transport.http.AxisServlet.doPost(AxisServlet.java:699)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
          at org.apache.axis.transport.http.AxisServletBase.service(AxisServletBase.java:327)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
          at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
          at org.jboss.web.tomcat.service.session.ClusteredSessionValve.handleRequest(ClusteredSessionValve.java:135)
          at org.jboss.web.tomcat.service.session.ClusteredSessionValve.invoke(ClusteredSessionValve.java:94)
          at org.jboss.web.tomcat.service.session.JvmRouteValve.invoke(JvmRouteValve.java:88)
          at org.jboss.web.tomcat.service.session.LockingValve.invoke(LockingValve.java:62)
          at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
          at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
          at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
          at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
          at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
          at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
          at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
          at java.lang.Thread.run(Thread.java:662)
  
  18:10:25,869 ERROR [JDBCTransaction] Could not toggle autocommit
  java.sql.SQLException: Closed Connection
          at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113)
          at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:147)
          at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:209)
          at oracle.jdbc.driver.PhysicalConnection.setAutoCommit(PhysicalConnection.java:1087)
          at org.apache.commons.dbcp.DelegatingConnection.setAutoCommit(DelegatingConnection.java:331)
          at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.setAutoCommit(PoolingDataSource.java:317)
          at org.hibernate.transaction.JDBCTransaction.toggleAutoCommit(JDBCTransaction.java:228)
          at org.hibernate.transaction.JDBCTransaction.commitAndResetAutoCommit(JDBCTransaction.java:173)
          at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:146)
          at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
          at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:752)
          at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:721)
          at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:394)
          at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:117)
          at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
          at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
          at $Proxy507.getInfoEmploye(Unknown Source)
          at com.dq.admincom.web.service.BalanceService.getInfoEmploye(BalanceService.java:32)
          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.axis.providers.java.RPCProvider.invokeMethod(RPCProvider.java:397)
          at org.apache.axis.providers.java.RPCProvider.processMessage(RPCProvider.java:186)
          at org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:323)
          at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
          at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
          at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
          at org.apache.axis.handlers.soap.SOAPService.invoke(SOAPService.java:454)
          at org.apache.axis.server.AxisServer.invoke(AxisServer.java:281)
          at org.apache.axis.transport.http.AxisServlet.doPost(AxisServlet.java:699)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
          at org.apache.axis.transport.http.AxisServletBase.service(AxisServletBase.java:327)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
          at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
          at org.jboss.web.tomcat.service.session.ClusteredSessionValve.handleRequest(ClusteredSessionValve.java:135)
          at org.jboss.web.tomcat.service.session.ClusteredSessionValve.invoke(ClusteredSessionValve.java:94)
          at org.jboss.web.tomcat.service.session.JvmRouteValve.invoke(JvmRouteValve.java:88)
          at org.jboss.web.tomcat.service.session.LockingValve.invoke(LockingValve.java:62)
          at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
          at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
          at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
          at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
          at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
          at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
          at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
          at java.lang.Thread.run(Thread.java:662)
  18:10:25,869 ERROR [JDBCTransaction] JDBC commit failed
  java.sql.SQLException: ORA-01012: not logged on
  
          at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113)
          at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
          at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:283)
          at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:278)
          at oracle.jdbc.driver.T4C7Ocommoncall.receive(T4C7Ocommoncall.java:141)
          at oracle.jdbc.driver.T4CConnection.doCommit(T4CConnection.java:539)
          at oracle.jdbc.driver.PhysicalConnection.commit(PhysicalConnection.java:1181)
          at org.apache.commons.dbcp.DelegatingConnection.commit(DelegatingConnection.java:301)
          at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.commit(PoolingDataSource.java:200)
          at org.hibernate.transaction.JDBCTransaction.commitAndResetAutoCommit(JDBCTransaction.java:170)
          at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:146)
          at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
          at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:752)
          at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:721)
          at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:394)
          at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:117)
          at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
          at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
          at $Proxy507.getInfoEmploye(Unknown Source)
          at com.dq.admincom.web.service.BalanceService.getInfoEmploye(BalanceService.java:32)
          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.axis.providers.java.RPCProvider.invokeMethod(RPCProvider.java:397)
          at org.apache.axis.providers.java.RPCProvider.processMessage(RPCProvider.java:186)
          at org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:323)
          at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
          at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
          at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
          at org.apache.axis.handlers.soap.SOAPService.invoke(SOAPService.java:454)
          at org.apache.axis.server.AxisServer.invoke(AxisServer.java:281)
          at org.apache.axis.transport.http.AxisServlet.doPost(AxisServlet.java:699)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
          at org.apache.axis.transport.http.AxisServletBase.service(AxisServletBase.java:327)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
          at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
          at org.jboss.web.tomcat.service.session.ClusteredSessionValve.handleRequest(ClusteredSessionValve.java:135)
          at org.jboss.web.tomcat.service.session.ClusteredSessionValve.invoke(ClusteredSessionValve.java:94)
          at org.jboss.web.tomcat.service.session.JvmRouteValve.invoke(JvmRouteValve.java:88)
          at org.jboss.web.tomcat.service.session.LockingValve.invoke(LockingValve.java:62)
          at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
          at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
          at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
          at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
          at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
          at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
          at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
          at java.lang.Thread.run(Thread.java:662)
  18:10:25,869 WARN  [JDBCExceptionReporter] SQL Error: 0, SQLState: null
  18:10:25,869 ERROR [JDBCExceptionReporter] Already closed.
  18:10:25,869 WARN  [JDBCExceptionReporter] SQL Error: 0, SQLState: null
  18:10:25,869 ERROR [JDBCExceptionReporter] Already closed.
  </code></pre>
  
  <p>my goal is to know how can I reestablish connection after being lost.</p>
  
  <p>I added this code in _BaseRootDAO :</p>
  
  <pre><code>protected Session getSession(String configFile) {
          if ((null != session) &amp;&amp; session.isOpen()) {
              return session;
          } else if (null != sessionFactory) {
              Session s = currentSession.get();
  
              if ((null == s) || !s.isOpen()) {
                  s = sessionFactory.openSession();
                  currentSession.set(s);
              } else {
                  try {
                      HibernateWork hibernateWork = new HibernateWork("SELECT 1 FROM DUAL");
                      s.doWork(hibernateWork);
                  } catch (Exception e) {
                      System.err.println("------ Connection is closed");
  
                      s = sessionFactory.openSession();
                      currentSession.set(s);
                  }
              }
  
              return s;
          } else {
              Session s = currentSession.get();
  
              if ((null == s) || !s.isOpen()) {
                  s = getSessionFactory(configFile).openSession();
                  currentSession.set(s);
              }
  
              return s;
          }
      }
  </code></pre>
  
  <p>and I make this config in applicationContext-db.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
  "http://www.springframework.org/dtd/spring-beans.dtd"&gt;
  
  &lt;beans&gt;
      &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;
        &lt;property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/&gt;
  
  
        &lt;property name="url" value="jdbc:oracle:thin:@192.168.0.22:1521:testSid"/&gt;
          &lt;property name="username" value="testUser"/&gt;  
          &lt;property name="password" value="testUser"/&gt;
           &lt;property name="maxActive" value="1500"/&gt;
          &lt;property name="maxIdle" value="1000"/&gt;
          &lt;property name="maxWait" value="20000"/&gt;
          &lt;property name="defaultAutoCommit" value="true"/&gt;
          &lt;property name="removeAbandoned" value="true"/&gt;
          &lt;property name="removeAbandonedTimeout" value="3600"/&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>