title: java.sql.SQLRecoverableException: Closed Connection
body: <p>I'm working on Struts 2 Framework with Hibernate 3.3 and using Oracle 11g. My web project was working fine since 5 months. But recently I'm facing the java.sql.SQLRecoverableException: Closed Connection during some idle time. I'll explain the scenario follows..
  My hibernate.cfg.xml configuration is</p>
  
  <pre><code>&lt;property name="hibernate.current_session_context_class"&gt;
          thread
      &lt;/propert
  &lt;property name="connection.url"&gt;jdbc:oracle:thin:@localhost:1521:orcl&lt;/property&gt;
  &lt;property name="connection.username"&gt;user&lt;/property&gt;
  &lt;property name="connection.password"&gt;user&lt;/property&gt;
  &lt;property name="connection.driver_class"&gt;
          oracle.jdbc.OracleDriver
      &lt;/property&gt;
  &lt;property name="myeclipse.connection.profile"&gt;Oracle&lt;/property&gt;
  &lt;property name="hibernate.connection.autoReconnect"&gt;true&lt;/property&gt;
  &lt;property name="dialect"&gt; org.hibernate.dialect.Oracle10gDialect &lt;/property&gt;
  </code></pre>
  
  <p>And my HibernateSessionFactory conf is</p>
  
  <pre><code>private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
  private static final ThreadLocal&lt;Session&gt; threadLocal = new ThreadLocal&lt;Session&gt;();
  private static Configuration configuration = new Configuration();
  private static org.hibernate.SessionFactory sessionFactory;
  private static String configFile = CONFIG_FILE_LOCATION;
  static {
      try {
          configuration.configure(configFile);
          sessionFactory = configuration.buildSessionFactory();
      } catch (Exception e) {
          System.err
                  .println("%%%% Error Creating SessionFactory %%%%");
          //e.printStackTrace();
      }
  }
  private HibernateSessionFactory() {
  }
  public static Session getSession() throws HibernateException {
      Session session = (Session) threadLocal.get();
  
      if (session == null || !session.isOpen()) {
          if (sessionFactory == null) {
              rebuildSessionFactory();
          }
          session = (sessionFactory != null) ? sessionFactory.openSession()
                  : null;
          threadLocal.set(session);
      }
  
      return session;
  }
  public static void rebuildSessionFactory() {
      try {
          configuration.configure(configFile);
          sessionFactory = configuration.buildSessionFactory();
      } catch (Exception e) {
          System.err
                  .println("%%%% Error Creating SessionFactory %%%%");
          //e.printStackTrace();
      }
  }
  public static void closeSession() throws HibernateException {
      Session session = (Session) threadLocal.get();
      threadLocal.set(null);
  
      if (session != null) {
          session.close();
      }
  }
  public static org.hibernate.SessionFactory getSessionFactory() {
      return sessionFactory;
  }
  public static void setConfigFile(String configFile) {
      HibernateSessionFactory.configFile = configFile;
      sessionFactory = null;
  }
  public static Configuration getConfiguration() {
      return configuration;
  }
  </code></pre>
  
  <p>Then the error comes when the following code runs</p>
  
  <pre><code>session = HibernateSessionFactory.getSession();
  Query query = session.createQuery(SQL_QUERY);
  try {
                  // session.connection().close();
                  System.out.println("CLOSED :"
                          + session.connection().isClosed());
                  if (session.connection().isClosed()) {
                      System.out.println("RECONNECTING.......");
                      session.reconnect();
                  }
                  System.out.println("CLOSED :"
                          + session.connection().isClosed());
              } catch (Exception e1) {
  
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
              }
  try {
  for (Iterator it = query.iterate(); it.hasNext();) {
                      chk = true;
                      ur = (EmagEnterpriseLogin) it.next();
  } catch (Exception e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  </code></pre>
  
  <p>When the above snippet runs even though able to session was opened and created query successfully, But fails at Iterating query. First time comes as</p>
  
  <pre><code>Caused By:java.sql.SQLRecoverableException: Closed Connection
  </code></pre>
  
  <p>But before this error in first try i captured the session.Connection.isClosed() was false. But after first error then keeps on coming as SQL State=null and from second time i could get session.Connection.isClosed() was true. And then reconnecting executes but still the same error repeats.I also tried rebuilding sessionfactory, that too failed. Please help me to solve this issue.</p>