Java 带有Spring3.1和Hibernate4注释的EHCache—使其正常工作
我正在尝试让EHCache在我的应用程序中工作。我做的第一件事是添加maven依赖项: pom.xmlJava 带有Spring3.1和Hibernate4注释的EHCache—使其正常工作,java,spring,hibernate,spring-mvc,ehcache,Java,Spring,Hibernate,Spring Mvc,Ehcache,我正在尝试让EHCache在我的应用程序中工作。我做的第一件事是添加maven依赖项: pom.xml <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>${hibernate-version}</version>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate-version}</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="jmxExporter"
class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="Hibernate:type=statistics">
<ref local="statisticsBean"/>
</entry>
</map>
</property>
</bean>
<bean id="statisticsBean" class="org.hibernate.jmx.StatisticsService">
<property name="statisticsEnabled" value="true"/>
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="namingStrategy" class="com.execon.OracleNamingStrategy"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="oracle.jdbc.OracleDriver"/>
<property name="jdbcUrl" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
<property name="user" value="xxx"/>
<property name="password" value="xxx"/>
<property name="maxPoolSize" value="10"/>
<property name="maxStatements" value="0"/>
<property name="minPoolSize" value="5"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="namingStrategy" ref="namingStrategy"/>
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<property name="packagesToScan" value="com.execon.models"/>
</bean>
</beans>
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</property>
<property name="hibernate.generate_statistics">true</property>
</session-factory>
</hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<diskStore path="java.io.tmpdir"/>
<defaultCache
eternal="false"
maxElementsInMemory="1000"
maxElementsOnDisk="10000"
overflowToDisk="true"
diskPersistent="true"
timeToLiveSeconds="300"
/>
</ehcache>
是时候定义hibernate.cfg.xml和ehcache文件了,它们是:
hibernate.cfg.xml
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate-version}</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="jmxExporter"
class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="Hibernate:type=statistics">
<ref local="statisticsBean"/>
</entry>
</map>
</property>
</bean>
<bean id="statisticsBean" class="org.hibernate.jmx.StatisticsService">
<property name="statisticsEnabled" value="true"/>
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="namingStrategy" class="com.execon.OracleNamingStrategy"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="oracle.jdbc.OracleDriver"/>
<property name="jdbcUrl" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
<property name="user" value="xxx"/>
<property name="password" value="xxx"/>
<property name="maxPoolSize" value="10"/>
<property name="maxStatements" value="0"/>
<property name="minPoolSize" value="5"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="namingStrategy" ref="namingStrategy"/>
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<property name="packagesToScan" value="com.execon.models"/>
</bean>
</beans>
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</property>
<property name="hibernate.generate_statistics">true</property>
</session-factory>
</hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<diskStore path="java.io.tmpdir"/>
<defaultCache
eternal="false"
maxElementsInMemory="1000"
maxElementsOnDisk="10000"
overflowToDisk="true"
diskPersistent="true"
timeToLiveSeconds="300"
/>
</ehcache>
正如您所看到的,这个基本方法alwas会返回相同的列表。因此,我正在检查hibernate统计信息,并:
secondLevelCacheHitCount 0
secondLevelCacheMissCount 0
secondLevelCachePutCount 0
屏幕上的其他统计信息:
链接如果太小:
那么,怎么了,我错过了什么(明显的)?还是我完全走错了路
编辑
结算模型组实体(也尝试了CacheConcurrencyStrategy.READ\u WRITE)
@实体
@表(name=“MODEL\u GROUP”)
@缓存(用法=CacheConcurrencyStrategy.TRANSACTIONAL)
公共类SettlementModelGroup实现可序列化
{
@身份证
@GeneratedValue(generator=“MODEL_GROUP_SEQ”,strategy=GenerationType.SEQUENCE)
@SequenceGenerator(name=“MODEL\u GROUP\u SEQ”,sequenceName=“SEQ\u MODEL\u GROUP\u MODEL\u GROUP\u ID”)
@列(name=“MODEL\u GROUP\u ID”,null=false)
私有整数modelId;
@列(name=“name”,nullable=false)
私有字符串modelGroupName;
@列(name=“DESCRIPTION”,nullable=false)
私有字符串模型组描述;
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“MODEL\u GROUP\u TYPE\u ID”,null=false)
私有结算模型组类型结算模型组类型;
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“PERIOD\u TYPE\u ID”,null=false)
私有周期型周期型;
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“DOMAIN\u ID”)
私有域;
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“OWNER\u ID”,nullable=false)
私人用户;
@OneToMany(fetch=FetchType.LAZY,mappedBy=“modelId”)
@级联(级联类型.ALL)
私有列表结算模型;
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“STATUS\u ID”)
私人身份;
//这里有接球手和接球手
}
将@Cache(用法=cacheconcurrencysttrategy.TRANSACTIONAL)
放在结算模型组(您的域实体)而不是服务方法上
也看到这个。根据您的EhCache版本(2.4.3.?),您可能必须使用CacheConcurrencyStrategy.READ\u WRITE.将@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
放在结算模型组(您的域实体)而不是服务方法上
也看到这个。根据您的EhCache版本(2.4.3.?),您可能必须使用CacheConcurrentyStrategy.READ_WRITE.是否将实体设置为可缓存?是的,尝试了这两种方法:org.springframework.cache.annotation.cacheable
和javax.persistence.cacheable
,没有任何变化。因此,请将查询设置为可缓存:session.createQuery(“from SettlementModelGroup”).setCacheable(true);然后尝试在同一方法中调用query.list()两次。它可以工作,但不是第二级缓存,而是第一级缓存(QueryCatchCount增加).这很好,但现在我打算做的是,因为就我而言,一级缓存与会话相连,对吗?因此,会话结束/过期后,它就消失了?是的,一级缓存就消失了。您是否将实体设置为可缓存?是的,尝试了这两种方法:org.springframework.cache.annotation.cacheable
和javax.persistence、 Cacheable
,无任何更改也可以将查询设置为Cacheable:session.createQuery(“来自结算模型组”).setCacheable(true);然后尝试在同一方法中调用query.list()两次。它可以工作,但不是二级缓存,而是一级缓存(QueryCatchCount增加).这很好,但现在我打算做的是,因为就我而言,一级缓存与会话相连,对吗?所以在会话结束/过期后,它就消失了?是的,一级缓存就消失了。它正在为除结算模型组
之外的所有其他实体工作,每次都从我上面发布的方法加载它。也许缓存集合时出现问题?请将域实体SettlementModelGroup添加到您的问题添加,它对除SettlementModelGroup
之外的所有其他实体都有效,每次都从我上面发布的方法加载它。可能缓存集合时出现问题?请添加域实体SettlementModelGroup对你的问题补充说,