我如何从entitymanager访问Hibernate统计信息? [英] How do I access Hibernate statistics from an entitymanager?

查看:148
本文介绍了我如何从entitymanager访问Hibernate统计信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < bean id =entityManagerFactoryclass = org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean > 
< property name =jpaDialect>
< bean class =org.collegeboard.springboard.core.jpa.HibernateJpaDialect>
< property name =flushModevalue =COMMIT/>
< / bean>
< / property>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter/>
< / property>
< property name =persistenceXmlLocationvalue =classpath:META-INF / test-persistence.xml/>
< property name =persistenceUnitNamevalue =orgTestingDatabase/>
< property name =dataSourceref =dataSource/>
< / bean>

< bean id =sharedEntityManagerclass =org.springframework.orm.jpa.support.SharedEntityManagerBean>
< property name =entityManagerFactoryref =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>
<! - 收集统计数据,这是为了测试缓存是否正常工作 - >
< property name =hibernate.generate_statistics> true< / property>

如何通过javax.persistence.EntityManager访问org.hibernate.stat.Statistics对象?显然,我需要以某种方式访问​​SessionFactory,但我无法弄清楚适当的系列剧演员。



谢谢, - Dave

解决方案

过去我曾与此搏斗过:



如果你只是想知道如果它正在工作,你可以为 org.hibernate.stat.Statistics org.hibernate.stat。* 启用Hibernate调试日志记录。 C $ C>。但是,如果你(像我)想要一个缓存统计报告,你可以做如下的事情。这暴露了一个包含所有统计信息的JMX bean:
$ b $ $ p $ $ $ $ $ $ $ $ $ $ b提供代码来注册Hibernate的二级缓存具有
* JMX MBean服务器的统计信息Bean。假设MBeanServer和
* EntityManagerFactory都作为Spring管理的bean提供。请注意,尽管注册此类的
*可以收集统计信息,即使先前禁用了
*也是如此。
* /
public class HibernateCacheStatisticsJmxRegistration {

@Autowired
private EntityManagerFactory entityManagerFactory;

@Autowired
private MBeanServer mbeanServer;

private ObjectName objectName;

/ **
*注册包装Hibernate会话工厂的统计MBean。
*
* @ throws JMException如果有任何失败..
* @see HibernateCacheStatisticsJmxRegistration#unregister()
* /
public void register()throws JMException {
final SessionFactory sessionFactory =((HibernateEntityManagerFactory)entityManagerFactory).getSessionFactory();

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

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

/ **
*注销已注册的MBean。
*
* @throws JMException如果注销失败
* @see HibernateCacheStatisticsJmxRegistration#register()
* /
public void unregister()throws JMException {
mbeanServer.unregisterMBean(objectName);






应用程序上下文:

 <! - 为各种缓存设置Ehcache管理器。 - > 
< bean id =ehCacheManagerclass =org.springframework.cache.ehcache.EhCacheManagerFactoryBean>
< property name =configLocationvalue =classpath:ehcache.xml/>
< / bean>
< ehcache:annotation-driven cache-manager =ehCacheManager/>

<! - 通过JMX公开缓存统计信息。 - >
< context:mbean-server />
< bean class =net.sf.ehcache.management.ManagementServiceinit-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 =HibernateCacheStatisticsJmxRegistrationinit-method =registerdestroy-method =unregister/>


I'm using Spring 3.1.1.RELEASE, JUnit 4.8.1, and Hibernate 4.1.5.Final. I'm trying to test whether my second level cache is configured correctly, but am unsure of how to do it. I'm using the JPA entity manager, configured in Spring like so ...

<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>

I have configured my second level cache like so ...

    <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>

How do I access a org.hibernate.stat.Statistics object given my javax.persistence.EntityManager ? Evidently, I need to access a SessionFactory somehow, but I can't figure out the appropriate series of casts.

Thanks, - Dave

解决方案

I was wrestling with this in the past: Exposing Hibernate (cache) statistics through JMX with Spring in Tomcat

If you simply want to know "if it's working" you could enable Hibernate debug logging for org.hibernate.stat.Statistics or org.hibernate.stat.*. However, if you (like I) want to have a cache statistics report you could do something like the following. This exposes a JMX bean with all the stats:

/**
 * 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);
  }
}

The app context:

<!-- 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" />

这篇关于我如何从entitymanager访问Hibernate统计信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆