1

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

<property name="hibernate.current_session_context_class">
        thread
    </propert
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">user</property>
<property name="connection.password">user</property>
<property name="connection.driver_class">
        oracle.jdbc.OracleDriver
    </property>
<property name="myeclipse.connection.profile">Oracle</property>
<property name="hibernate.connection.autoReconnect">true</property>
<property name="dialect"> org.hibernate.dialect.Oracle10gDialect </property>

And my HibernateSessionFactory conf is

private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
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;
}

Then the error comes when the following code runs

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();
            }

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

Caused By:java.sql.SQLRecoverableException: Closed Connection

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.

2
  • What is the oracle error? Commented Feb 7, 2013 at 18:38
  • org.hibernate.exception.JDBCConnectionException: could not execute query using iterate Commented Feb 7, 2013 at 19:29

1 Answer 1

2

Finally i found the solution for my problem is using third party Connection Provider instead of using Hibernate Native connection provider. I used c3p0 Connection provider , that also given me some problems and i solved that too. The reference link for c3p0 is here.. Hibernate c3p0 Connection NewPooledConnection close Exception

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.