3 Replies Latest reply on Jun 26, 2009 7:41 PM by Arbi Sookazian

    JMX MBean for Hibernate Statistics and Seam

    Arbi Sookazian Master

      I am following the instructions in this wiki:  My Link

      And have successfully installed the MBean for Hibernate Statistics (cool!) as per Publish a per session factory JMX MBean

      Now I need to complete the implementation for the 2nd part (When the SessionFactory is closed, we need to unregister the MBean)

      So that means I need to know when/how and which class/method actually calls entityManagerFactory.close();

      I didn't find this in the Seam core code for 2.0.2-FP except in org.jboss.seam.persistence.EntityManagerFactory but there's a comment there which states:

      A Seam component that bootstraps an EntityManagerFactory,
      for use of JPA outside of Java EE 5 / Embedded JBoss.

      I am running my app in JBoss 4.2.x.  So what is the best approach for solving this problem so that I can successfully unregister the MBean when the SessionFactory is closed?

      I'm hoping to @Observe a raised event if possible or something similarly easy.

      I am hoping to eventually integrate this solution into my HibernateUtil class.

        • 1. Re: JMX MBean for Hibernate Statistics and Seam
          Arbi Sookazian Master

          I shut down JBoss and seeing the following in console:

          09:48:18,743 INFO  [SessionFactoryImpl] closing

          So apparently the implementation of EntityManagerFactory interface is SessionFactoryImpl class when the persistence provider is Hibernate?

          Anyways, so must I use a Hibernate interceptor to solve this now?  How can I capture this event and continue my processing to unregister the MBean?

          • 2. Re: JMX MBean for Hibernate Statistics and Seam
            Arbi Sookazian Master

            Here is the code in org.hibernate.impl.SessionFactoryImpl class.  Note the last lines:

            observer.sessionFactoryClosed( this );

            Has anyone actually used this part of the Hibernate Core API for event tracking purposes?  thx.

                  * Closes the session factory, releasing all held resources.
                  * <ol>
                  * <li>cleans up used cache regions and "stops" the cache provider.
                  * <li>close the JDBC connection
                  * <li>remove the JNDI binding
                  * </ol>
                  * Note: Be aware that the sessionfactory instance still can
                  * be a "heavy" object memory wise after close() has been called.  Thus
                  * it is important to not keep referencing the instance to let the garbage
                  * collector release the memory.
                 public void close() throws HibernateException {
                      if ( isClosed ) {
                           log.trace( "already closed" );
                      isClosed = true;
                      Iterator iter = entityPersisters.values().iterator();
                      while ( iter.hasNext() ) {
                           EntityPersister p = (EntityPersister) iter.next();
                           if ( p.hasCache() ) {
                      iter = collectionPersisters.values().iterator();
                      while ( iter.hasNext() ) {
                           CollectionPersister p = (CollectionPersister) iter.next();
                           if ( p.hasCache() ) {
                      if ( settings.isQueryCacheEnabled() )  {
                           iter = queryCaches.values().iterator();
                           while ( iter.hasNext() ) {
                                QueryCache cache = (QueryCache) iter.next();
                      if ( settings.isAutoDropSchema() ) {
                           schemaExport.drop( false, true );
                      try {
                      finally {
                           SessionFactoryObjectFactory.removeInstance(uuid, name, properties);
                      observer.sessionFactoryClosed( this );

            • 3. Re: JMX MBean for Hibernate Statistics and Seam
              Arbi Sookazian Master

              Seam in Action does not list any Hibernate events as built-in raised events:

              Here’s a list of some of the events that
              Seam raises:
              ■ Seam container initialized
              ■ Context variable assignment (added, removed)
              ■ Component life-cycle events (created, destroyed)
              ■ Scope events (created, destroyed)
              ■ Authentication events
              ■ Transaction events (before complete, commit, rollback)
              ■ Exception events (handled, not handled)
              ■ Conversation, page flow, business process, and task boundaries
              ■ JSF phase transitions (before, after)
              ■ JSF validation failed
              ■ User preference change (theme, time zone, locale)

              Unless possibly component life-cycle event for this?  Not sure which component is involved here...