我正在使用Spring 3.1.1.RELEASE,JUnit 4.8.1和Hibernate 4.1.5.Final。我正在尝试测试二级缓存是否配置正确,但是不确定如何做。我正在使用像这样在Spring中配置的JPA实体管理器…
<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,但是我无法弄清楚适当的一系列转换。
谢谢-戴夫
过去我一直在努力:通过Tomcat中的Spring与JMX公开Hibernate(缓存)统计信息
如果您只是想知道“它是否在工作”,则可以为org.hibernate.stat.Statistics或启用Hibernate调试日志记录org.hibernate.stat.*。但是,如果您(像我一样)想要获得缓存统计信息报告,则可以执行以下操作。这将显示具有所有统计信息的JMX bean:
org.hibernate.stat.Statistics
org.hibernate.stat.*
/** * 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" />