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.