1 Reply Latest reply on Feb 9, 2010 3:58 PM by dgolovin

    Hibernate tools provides Hibernate statistics in Eclipse?

    asookazian

      Does Hibernate tools provide Hibernate statistics in Eclipse (perhaps in the Hibernate console)?

       

      I am currently using a JMX MBean to get dynamic data on Hibernate statistics for my EntityManager.

       

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

       

            @In EntityManager entityManager;
         
            public Statistics getStatistics() {   
                return ((HibernateSessionProxy)entityManager.getDelegate()).getSessionFactory().getStatistics();
            }
           
            public SessionFactory getSessionFactory() {
                return ((HibernateSessionProxy)entityManager.getDelegate()).getSessionFactory();
            }

       

      }

       

       

      @Name("jmxHibernateStatistics")
      @AutoCreate
      public class JmxHibernateStatistics {
         
          @In HibernateUtils hibernateUtils;
          @Logger Log log;

       

          public void installHibernateMBean() {
              Statistics stats = hibernateUtils.getStatistics();
              stats.logSummary();
              log.info("new Date(stats.getStartTime()) = "+new Date(stats.getStartTime()));
             
              String[] myQueries = stats.getQueries();
              for(int i = 0; i < myQueries.length; i++){
                  log.info("myQueries["+i+"]: "+myQueries[i]);
              }
             
              //get your SF
              SessionFactory sf = hibernateUtils.getSessionFactory();
              //get the available MBean servers
              List list = MBeanServerFactory.findMBeanServer(null);
              //take the first one
              MBeanServer server = (MBeanServer) list.get(0);

       

              //build the MBean name
              ObjectName on = null;
              try{
                  on = new ObjectName("Hibernate:type=statistics,application=sample");
              }
              catch(MalformedObjectNameException e){
                  log.error(e);
              }
              StatisticsService mBean = new StatisticsService();
              mBean.setSessionFactory(sf);
              try {
                  server.registerMBean(mBean, on);
              } catch (InstanceAlreadyExistsException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (MBeanRegistrationException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (NotCompliantMBeanException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }

       

              //WARNING:
             
              /*
               * When the SessionFactory is closed, we need to unregister the MBean

       

                  //get the available MBean servers
                  ArrayList list = MBeanServerFactory.findMBeanServer(null);
                  //take the first one
                  MBeanServer server = (MBeanServer) list.get(0);
                  server.unregisterMBean(on);
                 
                  You can add this code to you HibernateUtil like class.
               */
          }
      }