Help

Built with Seam

You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.

As per section 2.1.3 of JPA/Hibernate book, when enabling Hibernate statistics, Hibernate will expose a number of metrics that are useful when tuning a running system. This can be extremely helpful in trouble-shooting and performance analysis situations.

Add the following line to the properties section of your persistence.xml:

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

Create HibernateUtils class:

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;

@Name("hibernateUtils")
@AutoCreate
public class HibernateUtils {

  @In EntityManager entityManager;

  public Statistics getStatistics() {    
    return ((HibernateSessionProxy)entityManager.getDelegate()).getSessionFactory().getStatistics();
  }

}

Inject HibernateUtils into your backing bean and call logSummary() method:

HotelBookingAction:

 
@Stateful
@Name("hotelBooking")
public class HotelBookingAction implements HotelBooking {

@In
private EntityManager entityManager;

@In 
private HibernateUtils hibernateUtils;

@In(required=false) 
@Out
private Hotel hotel;

@Begin
   public void selectHotel(Hotel selectedHotel)
   {
      hotel = entityManager.merge(selectedHotel);

      hibernateUtils.getStatistics().logSummary();
   }
}

output:

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

NOTE: make sure the EntityManager instance name and case matches the value of the name attribute for the SMPC tag in components.xml:

   <persistence:managed-persistence-context name="entityManager"
                                     auto-create="true"
                      persistence-unit-jndi-name="java:/fooEntityManagerFactory"/>  

References:

http://www.hibernate.org/hib_docs/v3/api/org/hibernate/stat/Statistics.html

http://www.seamframework.org/Community/HibernateStatisticsAndJPA