If we use current_session_context_class as thread; Hibernate document says that once thread is DONE; hibernate automatically closes the session.
But what I noticed is that hibernate does an extra optimization. Hibernate closes the Session only if it is DIRTY (i.e. if any updates are done on it) else if session was used only for select queries ; it does not return session (DB connection ) to the pool. And reuse the session for next request getCurrentSession().
I am using c3p0 connection pool. To ensure reliable DB connection; I set all parameters like idleTime (see if connection is lieing idle in the pool), testConnectionOnCheckout (test connection whenever checksout from pool).
As hibernate gets the connection from pool and keep in its own cache until and unless the session gets dirty; hence connection pool is not able to close them after idleTime (though actually connection might be idle in Hibernate session pools) or not able to do testConnectionOnCheckout(as hibernate doesnt checkout from pool again and again.)
Hence, though connection pool feels everything is fine but still a connection in hibernate cache might be already invalid (MySQL closes connection after every 8 hrs)
Anyone faced this problem before ?
|