3 Replies Latest reply on Apr 25, 2009 9:48 AM by Arbi Sookazian

    Hibernate statistics and JPA

    Mark Wigmans Newbie

      I'am using jboss seam 2.1.0-SP1 with JPA and I want to enable hibernate statistics for performance analysis.

      The problem is that a hibernate StatisticsService wants to have the sessionFactory. How to retrieve this in a jboss seam 2 JPA environment?

      I tried it via @In private EntityManagerFactory entityManagerFactory; but this doens't work (remains null)

      Any ideas?

        • 1. Re: Hibernate statistics and JPA
          Julien Kronegg Novice

          I'm using something like:

          import org.hibernate.stat.Statistics;
          public class HibernateUtils {
            public static Statistics getStatistics() {
              EntityManager em = (EntityManager)Component.getInstance(EntityManager.class);
              return ((HibernateSessionProxy)em.getDelegate()).getSession(EntityMode.YOUR_ENTITY_MODE_HERE).getSessionFactory().getStatistics();

          Note: error checking and evident imports have been removed in the example above. You may want to add error messages, e.g. if there is no session or if the Delegate is not an HibernateSessionProxy.

          In the persistence.xml, I added the following property:

          <property name="hibernate.generate_statistics" value="true"/>

          • 2. Re: Hibernate statistics and JPA
            Julien Kronegg Novice

            Or instead of using the Session, you can simply use:


            • 3. Re: Hibernate statistics and JPA
              Arbi Sookazian Master

              I modded the booking example and got the following results:

              0:01,205 INFO  [StatisticsImpl] Logging statistics....
              0:01,206 INFO  [StatisticsImpl] start time: 1240643960318
              0:01,207 INFO  [StatisticsImpl] sessions opened: 5
              0:01,207 INFO  [StatisticsImpl] sessions closed: 3
              0:01,208 INFO  [StatisticsImpl] transactions: 3
              0:01,209 INFO  [StatisticsImpl] successful transactions: 3
              0:01,210 INFO  [StatisticsImpl] optimistic lock failures: 0
              0:01,211 INFO  [StatisticsImpl] flushes: 2
              0:01,213 INFO  [StatisticsImpl] connections obtained: 4
              0:01,214 INFO  [StatisticsImpl] statements prepared: 4
              0:01,215 INFO  [StatisticsImpl] statements closed: 4
              0:01,216 INFO  [StatisticsImpl] second level cache puts: 0
              0:01,217 INFO  [StatisticsImpl] second level cache hits: 0
              0:01,217 INFO  [StatisticsImpl] second level cache misses: 0
              0:01,218 INFO  [StatisticsImpl] entities loaded: 13
              0:01,219 INFO  [StatisticsImpl] entities updated: 0
              0:01,220 INFO  [StatisticsImpl] entities inserted: 0
              0:01,221 INFO  [StatisticsImpl] entities deleted: 0
              0:01,222 INFO  [StatisticsImpl] entities fetched (minimize this): 0
              0:01,223 INFO  [StatisticsImpl] collections loaded: 0
              0:01,224 INFO  [StatisticsImpl] collections updated: 0
              0:01,225 INFO  [StatisticsImpl] collections removed: 0
              0:01,226 INFO  [StatisticsImpl] collections recreated: 0
              0:01,228 INFO  [StatisticsImpl] collections fetched (minimize this): 0
              0:01,229 INFO  [StatisticsImpl] queries executed to database: 3
              0:01,229 INFO  [StatisticsImpl] query cache puts: 0
              0:01,231 INFO  [StatisticsImpl] query cache hits: 0
              0:01,231 INFO  [StatisticsImpl] query cache misses: 0
              0:01,233 INFO  [StatisticsImpl] max query time: 9ms


              package org.jboss.seam.example.booking;
              import org.hibernate.stat.Statistics;
              import javax.persistence.EntityManager;
              import org.jboss.seam.persistence.HibernateSessionProxy;
              import org.jboss.seam.annotations.Name;
              import org.jboss.seam.annotations.In;
              import org.jboss.seam.Component;
              import org.jboss.seam.annotations.AutoCreate;
              public class HibernateUtils {
                @In EntityManager entityManager;
                public Statistics getStatistics() {    
                  return ((HibernateSessionProxy)entityManager.getDelegate()).getSessionFactory().getStatistics();


                 public void selectHotel(Hotel selectedHotel)
                    hotel = em.merge(selectedHotel);