-->
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.  [ 3 posts ] 
Author Message
 Post subject: ThreadLocal pattern question under WebSphere.
PostPosted: Mon May 01, 2006 6:37 pm 
Regular
Regular

Joined: Tue Mar 21, 2006 11:01 am
Posts: 65
Billy Newport, of IBM recommends against the use of ThreadLocal with WebSphere:

[url]http://www.devwebsphere.com/devwebsphere/2005/06/dont_use_thread.html
[/url]
As a comment to this article, Gavin King of Hibernate points out that Hibernate does not use ThreadLocal, which is true. But Hibernate certainly advocates the use of ThreadLocal by its users, for example, in the CaveatEmptor sample.

[url]http://cvs.sourceforge.net/viewcvs.py/hibernate/CaveatEmptor/HiA/src/java/org/hibernate/auction/persistence/HibernateUtil.java?rev=1.2&view=markup
[/url]

Newport, in the above article, says:

Quote:
You may get away with using ThreadLocal on a thread but as soon as you return and give that thread back to the application server then you can't assume:

1. You'll get called again on the same thread.
2. Even if you do get the same thread, it may have different threadlocal values as it may have been used by other applications.


I'm not concerned about point 1 but I am concerned about point 2. In particular, I'm concerned about this snippet of code from the CaveatEmptor HibernateUtil sample:



Code:
   /**
    * Retrieves the current Session local to the thread.
    * <p/>
    * If no Session is open, opens a new Session for the running thread.
    *
    * @return Session
    */
   public static Session getSession()
      throws InfrastructureException {
      Session s = (Session) threadSession.get();
      try {
         if (s == null) {
            log.debug("Opening new Session for this thread.");
            if (getInterceptor() != null) {
               log.debug("Using interceptor: " + getInterceptor().getClass());
               s = getSessionFactory().openSession(getInterceptor());
            } else {
               s = getSessionFactory().openSession();
            }
            threadSession.set(s);
         }
      } catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      }
      return s;
   }

   /**
    * Closes the Session local to the thread.
    */
   public static void closeSession()
      throws InfrastructureException {
      try {
         Session s = (Session) threadSession.get();
         threadSession.set(null);
         if (s != null && s.isOpen()) {
            log.debug("Closing Session of this thread.");
            s.close();
         }
      } catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      }
   }


If Newport is right that we should worry about threads being reused by the pool, is the above code not dangerous? Thread A uses session, closes using closeSession(). Later Thread B comes along, gets the same thread from the pool without expecting it, calls getSession(), finds the thread isn't null, but it's been closed, and therefore will fail.

I haven't experienced this yet, my application isn't written yet, but can someone tell me if I should shy away from using ThreadLocal in this Hibernate-recommended way, if I am also running under WebSphere? Or will I be okay, but perhaps need to make the getSession() member look for a closed session and react accordingly?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 01, 2006 7:25 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 02, 2006 9:54 am 
Regular
Regular

Joined: Tue Mar 21, 2006 11:01 am
Posts: 65
Thanks. This is interesting but doesn't really answer my question. And I suppose I should have mentioned that I'm restricted to using Hibernate 3.0.5, so parts of this article don't apply.


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

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.