-->
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: HibernateFilter and Session.close
PostPosted: Mon Oct 30, 2006 2:07 pm 
Regular
Regular

Joined: Fri Sep 30, 2005 6:15 am
Posts: 50
Hi,

I use the famous pattern to use Hibernate in Struts app.
To simplify this pattern, it's like this:

HibernateUtil.java
Code:
public static final ThreadLocal sessionThread = new ThreadLocal();

   public static Session currentSession() throws HibernateException {
      Session session = (Session) sessionThread.get();
      // Open a new Session, if this Thread has none yet
      if (session == null) {
         session = sessionFactory.openSession();
         sessionThread.set(session);
      }
      return session;
   }

   public static void closeSession() throws HibernateException {
      Session session = (Session) sessionThread.get();
      sessionThread.set(null);
      if (session != null){
         session.close();
      }    
   }


HibernateFilter.java
Code:
   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

      try {

         // Call the next filter (continue request processing)
         chain.doFilter(request, response);

      } finally {
         // No matter what happens, close the Session.
         [color=red][b]HibernateUtil.closeSession();[/b][/color]
      }
   }


My problem, is that we close the session in the filter but what if session is using by another client that is executing a transaction?

I have done the test with two simultaneous call to the same servlet, so two transactions are opened into the same session but when the first transaction ends, it closes the sessions so that it rollbacks my second transaction that wasn't completed!

Do I say wrong?
Am I coding something wrong?
please could you help me....
(note: to be able to reproduce the problem, I have created two transactions that takes about 20sec to complete, otherwise, the first call could closes the session before the second open it!)

Thanks for your help


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.