-->
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.  [ 2 posts ] 
Author Message
 Post subject: CaveatEmptor HibernateUtil and ThreadLocal problems
PostPosted: Thu Jun 08, 2006 11:22 am 
Regular
Regular

Joined: Tue Mar 21, 2006 11:01 am
Posts: 65
Hibernate version: 3.0.5

I am accessing Hibernate through a HibernateUtil class patterned after the one on CaveatEmptor. The portions where I am having trouble are identical to what is on CaveatEmptor (see below)


Code:
package org.hibernate.auction.persistence;

// import ...

public class HibernateUtil {

   private static Log log = LogFactory.getLog(HibernateUtil.class);

   private static Configuration configuration;
   private static SessionFactory sessionFactory;

   private static final ThreadLocal threadSession = new ThreadLocal();
   private static final ThreadLocal threadTransaction = new ThreadLocal();
   private static final ThreadLocal threadInterceptor = new ThreadLocal();

// ... session factory stuff omitted for brevity


   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);
      }
   }

   /**
    * Start a new database transaction.
    */
   public static void beginTransaction()
      throws InfrastructureException {
      Transaction tx = (Transaction) threadTransaction.get();
      try {
         if (tx == null) {
            log.debug("Starting new database transaction in this thread.");
            tx = getSession().beginTransaction();
            threadTransaction.set(tx);
         }
      } catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      }
   }

//   ...
}


Basically there is a JUnit test that passes without problem when run standalone. When the test is placed inside a test suite, whichever test comes second fails when it tries to commit a transaction. The reason it fails is that the beginTransaction method finds an existing transaction that is tied to a different (closed) session which then fails with a "Session is closed" message.

Basically the jvm inside Eclipse which runs the tests is reusing the thread, so the threadLocals are stale. The HibernateUtil code (and mine, which is identical) treat the threadTransaction and threadSession variables as independent identities.

My question is this:
Is it reasonable to assume that the lifeline of a Transaction will be always entirely with the lifeline of a Session? If so, then it would be safe to rewrite the closeSession() method as follows:

Code:
   public static void closeSession()
      throws InfrastructureException {
      try {
         Session s = (Session) threadSession.get();
         threadSession.set(null);

                        // THIS IS MY SUGGESTED ADDITION
                        threadTransaction.set(null);
                        //END OF SUGGESTED ADDITION

         if (s != null && s.isOpen()) {
            log.debug("Closing Session of this thread.");
            s.close();
         }
      } catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      }
   }

This would eliminate the possibility of a stale transaction being found. Would it have negative effects? Am I overlooking something?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 08, 2006 12:14 pm 
Regular
Regular

Joined: Tue Mar 21, 2006 11:01 am
Posts: 65
Alternatively (I like this better - in finally clause): Same questions apply.

Code:
   public static void closeSession()
      throws InfrastructureException {
      try {
         Session s = (Session) threadSession.get();
          if (s != null && s.isOpen()) {
            log.debug("Closing Session of this thread.");
            s.close();
         }
      } catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      } finally {
        threadSession.set(null);
        threadTransaction.set(null);
      }
   }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.