4 Replies Latest reply on Aug 30, 2007 8:45 AM by bvogt

    hibernate session handling

    bvogt

      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.


        • 1. Re: hibernate session handling

          We use a configuration of hibernate that automatically close the session when the JTA transaction commits or rollbacks.

          • 2. Re: hibernate session handling
            bvogt

            and where is the commit/rollback initiated (explicitly/implicitly)?

            for the example below, in: core\src\resources\portal-core-sar\conf\hibernate\user\hibernate.cfg.xml

            I cannot find something related to that. Is it done somewhere else?

            Thanks for the time you spend on this!

            • 3. Re: hibernate session handling

              This is done programmatically in org.jboss.portal.jems.hibernate.SessionFactoryBinder

               // Force transaction manager lookup class and JTA env
               setPropertyIfAbsent("transaction.auto_close_session", "true");
               setPropertyIfAbsent("transaction.flush_before_completion", "true");
               setPropertyIfAbsent("hibernate.transaction.flush_before_completion", "true");
               setPropertyIfAbsent("hibernate.transaction.factory_class", "org.hibernate.transaction.JTATransactionFactory");
               setPropertyIfAbsent("hibernate.transaction.manager_lookup_class", "org.hibernate.transaction.JBossTransactionManagerLookup");
              


              Transaction demarcation occurs in the server interceptor org.jboss.portal.core.aspects.server.TransactionInterceptor.java, the transaction demarcation is delegated to a JBoss AOP aspect which applies on the TransactionInterceptor, found in portal-aop.xml

               <metadata
               tag="transaction"
               class="org.jboss.portal.core.aspects.server.TransactionInterceptor">
               <method name="invoke">
               <trans-attribute>RequiresNew</trans-attribute>
               </method>
               </metadata>
              


              • 4. Re: hibernate session handling
                bvogt

                Thanks for that! We're fighting against a serious MySQL exception bug, which requires an AS restart each day/night.