Version 6

    A Seam Managed Persistence Context can be initated to collect the Hibernate Statistics.  One way to do this is by creating a Seam component that is Application Scoped and is initiated as soon as the Application is started using the @Startup annotation.  This Seam component is used to initate the StatisticsService (https://www.hibernate.org/hib_docs/v3/api/org/hibernate/jmx/StatisticsService.html)

    with the Seam Managed Persistence Context's Hibernate Session Factory.

     

    Here is the Seam component named hibernateStatistics:

     

     

    @Name("hibernateStatistics")
    @Scope(ScopeType.APPLICATION)
    @Startup
     
    public class HibernateUtils {
       @In 
             private  EntityManager em;
    
       @Create
      public void onStartup(){
         if (em != null) {
         try {
      
           stats = ((HibernateSessionProxy)em.getDelegate()).getSessionFactory().getStatistics();
           server = (MBeanServer) MBeanServerFactory.findMBeanServer(null).get(0);
           StatisticsService mBean = new StatisticsService();
           ObjectName objectName = new ObjectName("Hibernate:type=statistics,application=RHQ");
           server.registerMBean(mBean, objectName);
           mBean.setSessionFactory(((HibernateSessionProxy)em.getDelegate()).getSessionFactory());
         } catch (Exception e){
           throw new RuntimeException("The persistence context " + em.toString() +
                "is not properly      configured.", e);
         }
      }
     }
     }
    
    

    The Components.xml need to reference the Seam Managed Persistence Context:

     

    ...
    <persistence:managed-persistence-context auto-create="true"
      entity-manager-factory="#{hibernateHelperEntityManagerFactory}" name="em"/>
     <persistence:entity-manager-factory
      name="hibernateHelperEntityManagerFactory" persistence-unit-name="bookingDatabase"/>
    ...
    


    The persistence.xml will need to define the persistence unit bookingDatabase and set the hibernate.generate_statistics property to true:

    ...

    <persistence-unit name="bookingDatabase">
          <provider>org.hibernate.ejb.HibernatePersistence</provider>
          <jta-data-source>java:/bookingDatasource</jta-data-source>
          <properties>
             <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
             <property name="hibernate.show_sql" value="true"/>
             <property name="hibernate.format_sql" value="true"/>
             <property name="hibernate.default_schema" value="PUBLIC"/>
             <property name="hibernate.generate_statistics" value="true"/>
             
             <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
             <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
          </properties>
    ...
    

     

    After the application is built and deployed the statistics can be viewed for all the entities that use this Seam Managed Persistence Context in the the JBoss Operations Network(JON), jconsole, the EAP 5 Admin Console or any other mbean viewer.

     

    There is another way to obtain these statistics without creating a Startup class, this uses an mbean, although Seam Managed Persistence Contexts are a Best Practice, the alternate method is preferred since it will work when Seam Managed Persistence Contexts are not used and for that matter it is not dependent on Seam.