-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: long conversation pattern - session.isConnected()
PostPosted: Sat Oct 10, 2009 3:26 pm 
Beginner
Beginner

Joined: Tue Sep 08, 2009 9:49 am
Posts: 23
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#A5

Image

My 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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.