Hi,
using Hibernate 3.2
PostgreSQL 8.4
I'm implementing the long conversation pattern (also known as extended or long session pattern) as described here:
https://www.hibernate.org/43.html#A5My application is a JSF webapp.
I adapted the example code from the article to fit into a JSF phase listener, which is almost the same like a servlet filter.
As far as I understood that pattern, it should disconnect the current sessions JDBC connection, or release it to the thread pool. But when I check the connection state with session.isConnected() it always gives back TRUE. Even if I do something like:
Code:
...
session.disconnect();
if(session.isConnected())
System.out.println("session is connected");
...
Maybe someone can give me a hint why the disconnect does not work.
Here is my code (I store the hibernate session in a JSF 'session'-scoped backing bean named HibernateSessionBinderBean):
Code:
public class HibernateSessionConnectionPhaseListener implements PhaseListener
{
private static final long serialVersionUID = -3194729122933784404L;
@Override
public void afterPhase(PhaseEvent arg0)
{
if(arg0.getPhaseId() == PhaseId.RESTORE_VIEW) //open db connection here
{
Session session = HibernateSessionBinderBean.getInstance().getSession();
if(session == null)
{
//create new session
session = HibernateSessionManager.getOpenNewSession();
HibernateSessionBinderBean.getInstance().setSession(session);
}
ManagedSessionContext.bind((org.hibernate.classic.Session)session);
}
if(arg0.getPhaseId() == PhaseId.RENDER_RESPONSE) //close db connection here
{
Session session = HibernateSessionBinderBean.getInstance().getSession();
session = ManagedSessionContext.unbind(HibernateSessionManager.getSessionFactory());
if(session.getTransaction().isActive())
session.getTransaction().commit();
HibernateSessionBinderBean.getInstance().setSession(session);
}
}
@Override
public void beforePhase(PhaseEvent arg0)
{
this.toString();
}
@Override
public PhaseId getPhaseId()
{
return PhaseId.ANY_PHASE;
}
}
Here is the related part of my hibernate.cfg.xml:
Code:
<session-factory>
<property name="dialect">org.hibernatespatial.postgis.PostgisDialect</property>
<!-- get connection pool from tomcat jndi, configured in the tomcat/conf/server.xml file -->
<property name="connection.datasource">java:comp/env/jdbc/postgres</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="max_fetch_depth">1</property>
<!-- prints out the generated SQL commands -->
<property name="hibernate.show_sql">true</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">org.hibernate.context.ManagedSessionContext</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
....mappings...
regards
Humppa