3

我正在使用 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

4 回答 4

5

我过去一直在努力解决这个问题:通过 JMX 和 Tomcat 中的 Spring 公开 Hibernate (cache) statistics

如果您只是想知道“它是否正常工作”,您可以为org.hibernate.stat.Statistics或启用 Hibernate 调试日志记录org.hibernate.stat.*。但是,如果您(像我一样)想要一个缓存统计报告,您可以执行以下操作。这会公开一个包含所有统计信息的 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" />
于 2013-01-01T13:20:27.430 回答
2

实体管理器有一个名为 getDelegate 的方法。调用它并将其类型转换为休眠会话对象。拥有休眠会话对象后,您可以获得会话工厂和统计信息。

于 2013-01-01T16:33:03.717 回答
2

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

以编程方式

如果要Statistics在应用程序中获取对象,可以按如下方式进行:

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

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

首先,我们将 解包EntityManager到 HibernateSession中,然后您可以Syatistics通过关联的SesisonFactory.

日志记录

如果也可以记录Statistics报告,这种情况下需要添加如下日志配置项:

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

JMX

对于 Hibernate 5.4.2 及更高版本,您还可以Statistics通过 JMX 公开对象。

为此,您需要设置hibernate.jmx.enabled配置属性:

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

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

于 2019-05-07T08:15:28.017 回答
0

最简单的方法是为您定义一个 bean,然后将其自动装配。据我所知,SessionFactory没有直接的方法可以SessionFactory从.EntityManager

于 2012-12-31T23:03:54.933 回答