[jboss-dev-forums] [Design of JBoss Portal] - hibernate session handling

bvogt do-not-reply at jboss.com
Thu Aug 30 05:35:07 EDT 2007


I do not see, that hibernate session are closed, is the really not necessary?

looking at portals sources I can see for example:
   public User findUserByUserName(String userName) throws IdentityException
  |    {
  |       if (userName != null)
  |       {
  |          try
  |          {
  |             Session session = getCurrentSession();
  |             Query query = session.createQuery("from HibernateUserImpl where userName=:userName");
  |             query.setParameter("userName", userName);
  |             query.setCacheable(true);
  |             HibernateUserImpl user = (HibernateUserImpl)query.uniqueResult();
  |             if (user == null)
  |             {
  |                throw new NoSuchUserException("No such user " + userName);
  |             }
  |             return user;
  |          }
  |          catch (HibernateException e)
  |          {
  |             String message = "Cannot find user by name " + userName;
  |             log.error(message, e);
  |             throw new IdentityException(message, e);
  |          }
  |       }
  |       else
  |       {
  |          throw new IllegalArgumentException("user name cannot be null");
  |       }
  |    }
  | 

looking at the hibernate doc I read:
A typical transaction should use the following idiom: 
  | 
  |  Session sess = factory.openSession();
  |  Transaction tx;
  |  try {
  |      tx = sess.beginTransaction();
  |      //do some work
  |      ...
  |      tx.commit();
  |  }
  |  catch (Exception e) {
  |      if (tx!=null) tx.rollback();
  |      throw e;
  |  }
  |  finally {
  |      sess.close();
  |  }
  |  
  | If the Session throws an exception, the transaction must be rolled back and the session discarded. The internal state of the Session might not be consistent with the database after the exception occurs. 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4079504#4079504

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4079504



More information about the jboss-dev-forums mailing list