My application is in idle state since yesterday night. Today its giving me JDBCConnectionException with SQL state 08S01. In this situtaion, I'd like to reconnect the session.
Below is the code I use to get current session. Multiple sessionfactory are maintained outside since the application deals with multiple databases. Before returning the Session handle, it calls isConnected() and reconnect if required. But when I was debugging the code, it showed me that session is connected(even underlying connection may have closed) and later on when that session was used, it threw JDBCConnectionException.
Now my question is how do I make sure that database connection is established before using Session object and if the connection is not present, reconnect. This will gurantee that at any point of time, any JDBCConnectionException won't occur(unless database is down or change in password)
Code:
public static Session currentSession(SessionFactory sessionFactory)
{
Session s = (Session) session.get();
if (s == null)
{
s = sessionFactory.openSession();
session.set(s);
}
else
{
if (!s.isConnected())
{
closeSession();
s = sessionFactory.openSession();
session.set(s);
}
}
if(!s.isConnected())
s.reconnect();
return s;
}
Please help !