如何从实体管理器访问Hibernate统计信息?

时间:2012-12-31 22:38:34

标签: spring hibernate junit second-level-cache sessionfactory

我正在使用Spring 3.1.1.RELEASE,JUnit 4.8.1和Hibernate 4.1.5.Final。我正在尝试测试我的二级缓存是否配置正确,但我不确定如何做到这一点。我正在使用JPA实体管理器,在Spring中配置如此...

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jpaDialect">
        <bean class="org.collegeboard.springboard.core.jpa.HibernateJpaDialect">
            <property name="flushMode" value="COMMIT"/>
        </bean>
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
    </property>
    <property name="persistenceXmlLocation" value="classpath:META-INF/test-persistence.xml"/>
    <property name="persistenceUnitName" value="orgTestingDatabase"/>
    <property name="dataSource" ref="dataSource"/>
</bean>

<bean id="sharedEntityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
   <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

我已经配置了我的二级缓存...

    <property name="hibernate.cache.use_second_level_cache">true</property>
    <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
    <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> 
    <!--  Collect stats, this is for testing if the cache is working -->
    <property name="hibernate.generate_statistics">true</property>

如何在给定javax.persistence.EntityManager的情况下访问org.hibernate.stat.Statistics对象?显然,我需要以某种方式访问​​SessionFactory,但我无法找出适当的强制转换系列。

谢谢, - 戴夫

4 个答案:

答案 0 :(得分:5)

过去我正在努力解决这个问题:Exposing Hibernate (cache) statistics through JMX with Spring in Tomcat

如果您只是想知道“它是否正常工作”,您可以为org.hibernate.stat.Statisticsorg.hibernate.stat.*启用Hibernate调试日志记录。但是,如果您(像我)想要一个缓存统计报告,您可以执行以下操作。这会公开一个包含所有统计信息的JMX bean:

/**
 * Provides code to register Hibernate's 2nd level cache statistics bean with a
 * JMX MBean server. Assumes that both the MBeanServer and the
 * EntityManagerFactory are available as Spring-managed beans. Note that while
 * registering this class enables the collection of statistics even if that was
 * previously disabled.
 */
public class HibernateCacheStatisticsJmxRegistration {

  @Autowired
  private EntityManagerFactory entityManagerFactory;

  @Autowired
  private MBeanServer mbeanServer;

  private ObjectName objectName;

  /**
   * Registers the statistics MBean that wraps a Hibernate session factory.
   * 
   * @throws JMException if anything fails..
   * @see HibernateCacheStatisticsJmxRegistration#unregister()
   */
  public void register() throws JMException {
    final SessionFactory sessionFactory = ((HibernateEntityManagerFactory) entityManagerFactory).getSessionFactory();

    objectName = new ObjectName("net.sf.ehcache:type=CacheStatistics,name=Hibernate2ndLevelCache");

    final StatisticsService statsMBean = new StatisticsService();
    statsMBean.setSessionFactory(sessionFactory);
    statsMBean.setStatisticsEnabled(true);
    mbeanServer.registerMBean(statsMBean, objectName);
  }

  /**
   * Unregisters the MBean that was registered.
   * 
   * @throws JMException if the de-registration fails
   * @see HibernateCacheStatisticsJmxRegistration#register()
   */
  public void unregister() throws JMException {
    mbeanServer.unregisterMBean(objectName);
  }
}

应用程序上下文:

<!-- Setting up Ehcache manager for various caches. -->
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
  <property name="configLocation" value="classpath:ehcache.xml" />
</bean>  
<ehcache:annotation-driven cache-manager="ehCacheManager" />

<!-- Exposing cache statistics through JMX. -->
<context:mbean-server />
<bean class="net.sf.ehcache.management.ManagementService" init-method="init">
  <constructor-arg ref="ehCacheManager"/>
  <constructor-arg ref="mbeanServer"/>
  <constructor-arg value="true"/>
  <constructor-arg value="true"/>
  <constructor-arg value="true"/>
  <constructor-arg value="true"/>
</bean>    
<bean class="HibernateCacheStatisticsJmxRegistration" init-method="register" destroy-method="unregister" />

答案 1 :(得分:2)

实体管理器有一个名为getDelegate的方法。调用它并键入强制转换为hibernate会话对象。一旦你有了hibernate会话对象,你就可以得到会话工厂和统计数据。

答案 2 :(得分:1)

您可以通过多种方式访问​​休眠统计信息:

以编程方式

如果要在应用程序中获取Statistics对象,可以按以下步骤进行操作:

Session session = entityManager.unwrap(Session.class);

Statistics statistics = session.getSessionFactory().getStatistics();

首先,我们将EntityManager解包到休眠Session,然后您可以通过关联的Syatistics访问SesisonFactory

记录

如果还可以记录Statistics报告,则在这种情况下,您需要添加以下日志配置条目:

<logger name="org.hibernate.engine.internal.StatisticalLoggingSessionEventListener" level="info"/>

JMX

对于Hibernate 5.4.2和更高版本,您还可以通过JMX公开Statistics对象,如this article中所述。

为此,您需要设置以下配置属性:

<property name="hibernate.jmx.enabled" value="true"/>

并在JMX客户端应用程序中找到org.hibernate.core MBean软件包。

如果您想更好地了解Hibernate支持的指标,请查看this article

答案 3 :(得分:0)

最简单的方法是为您的SessionFactory定义一个bean并将其自动加入。就我所知,没有直接的方式从SessionFactory获取EntityManager