二级缓存在 Hibernate 中不活跃?
Second Level Cache isn't active in Hibernate?
我正在研究 Hibernate 中的 Second Hibernate Cache。我有一个问题,我花了更多的时间去探索,但没有。
- 我使用 Oracle,并通过注释创建 table。
hibernate.cfg.xml:
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:quan</property>
<property name="connection.username">system</property>
<property name="connection.password">123456</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- Enable Second Level Cache -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="net.sf.ehcache.configurationResourceName">ehcache.xml</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.javamakeuse.poc.pojo.Employee"/>
</session-factory>
主要class:
Transaction t=null;
t=s.beginTransaction();
Employee e=(Employee)s.get(Employee.class, new Integer(1));
if(e!=null){
System.out.print("ID :"+e.getEmpid());
System.out.print(", Name :"+e.getEmpname());
System.out.print(", Phone :"+e.getEmpmobile());
System.out.println("Salary :"+e.getEmpsalary());
}else{
System.out.println("No records:!!");
}
//// second time loading same entity from the first level cache
e=(Employee)s.get(Employee.class, new Integer(1));
if(e!=null){
System.out.print("ID :"+e.getEmpid());
System.out.print(", Name :"+e.getEmpname());
System.out.print(", Phone :"+e.getEmpmobile());
System.out.println("Salary :"+e.getEmpsalary());
}else{
System.out.println("No records:!!");
}
// remove Cache
s.evict(e);
// Going to print Employee*** from Second level Cache
// Why i have a new query???????????????????
e=(Employee)s.get(Employee.class, new Integer(1));
if(e!=null){
System.out.print("ID :"+e.getEmpid());
System.out.print(", Name :"+e.getEmpname());
System.out.print(", Phone :"+e.getEmpmobile());
System.out.println("Salary :"+e.getEmpsalary());
}else{
System.out.println("No records:!!");
}
t.commit();
ehcache.xml
<ehcache>
<defaultCache maxEntriesLocalHeap="4000" eternal="false"
timeToIdleSeconds="60" timeToLiveSeconds="120" diskSpoolBufferSizeMB="20"
maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" statistics="true">
<persistence strategy="localTempSwap" />
</defaultCache>
<cache name="country" maxEntriesLocalHeap="4000" eternal="false"
timeToIdleSeconds="5" timeToLiveSeconds="10">
<persistence strategy="localTempSwap" />
</cache>
<cache name="org.hibernate.cache.spi.UpdateTimestampsCache"
maxEntriesLocalHeap="5000" eternal="true">
<persistence strategy="localTempSwap" />
</cache>
控制台输出
ID :1Hibernate:
select
employee0_.empid as empid1_0_0_,
employee0_.empmobile as empmobile2_0_0_,
employee0_.empname as empname3_0_0_,
employee0_.empsalary as empsalary4_0_0_
from
employee2 employee0_
where
employee0_.empid=?
Name :Nguyen VQ, Phone :948380166Salary :6.2
ID :1, Name :Nguyen VQ, Phone :948380166Salary :6.2
ID :1Hibernate:
select
employee0_.empid as empid1_0_0_,
employee0_.empmobile as empmobile2_0_0_,
employee0_.empname as empname3_0_0_,
employee0_.empsalary as empsalary4_0_0_
from
employee2 employee0_
where
employee0_.empid=?
Name :Nguyen VQ, Phone :948380166Salary :6.2
ID4: 1,Name4 :Nguyen VQ,Mobile: 948380166, Salary: 6.2
当我通过 s.evict(e) 删除 e 时;然后是 SessionFactory,但它不工作(它创建新查询)
二级配置似乎设置正确。我怀疑的一件事是 @Cache
属性。您是否使用 @Cache
注释在 Employee 对象上启用了 CacheConcurrencyStrategy。
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE | NONSTRICT_READ_WRITE | TRANSACTIONAL)
我正在研究 Hibernate 中的 Second Hibernate Cache。我有一个问题,我花了更多的时间去探索,但没有。
- 我使用 Oracle,并通过注释创建 table。
hibernate.cfg.xml:
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:quan</property>
<property name="connection.username">system</property>
<property name="connection.password">123456</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- Enable Second Level Cache -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="net.sf.ehcache.configurationResourceName">ehcache.xml</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.javamakeuse.poc.pojo.Employee"/>
</session-factory>
主要class:
Transaction t=null;
t=s.beginTransaction();
Employee e=(Employee)s.get(Employee.class, new Integer(1));
if(e!=null){
System.out.print("ID :"+e.getEmpid());
System.out.print(", Name :"+e.getEmpname());
System.out.print(", Phone :"+e.getEmpmobile());
System.out.println("Salary :"+e.getEmpsalary());
}else{
System.out.println("No records:!!");
}
//// second time loading same entity from the first level cache
e=(Employee)s.get(Employee.class, new Integer(1));
if(e!=null){
System.out.print("ID :"+e.getEmpid());
System.out.print(", Name :"+e.getEmpname());
System.out.print(", Phone :"+e.getEmpmobile());
System.out.println("Salary :"+e.getEmpsalary());
}else{
System.out.println("No records:!!");
}
// remove Cache
s.evict(e);
// Going to print Employee*** from Second level Cache
// Why i have a new query???????????????????
e=(Employee)s.get(Employee.class, new Integer(1));
if(e!=null){
System.out.print("ID :"+e.getEmpid());
System.out.print(", Name :"+e.getEmpname());
System.out.print(", Phone :"+e.getEmpmobile());
System.out.println("Salary :"+e.getEmpsalary());
}else{
System.out.println("No records:!!");
}
t.commit();
ehcache.xml
<ehcache>
<defaultCache maxEntriesLocalHeap="4000" eternal="false"
timeToIdleSeconds="60" timeToLiveSeconds="120" diskSpoolBufferSizeMB="20"
maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" statistics="true">
<persistence strategy="localTempSwap" />
</defaultCache>
<cache name="country" maxEntriesLocalHeap="4000" eternal="false"
timeToIdleSeconds="5" timeToLiveSeconds="10">
<persistence strategy="localTempSwap" />
</cache>
<cache name="org.hibernate.cache.spi.UpdateTimestampsCache"
maxEntriesLocalHeap="5000" eternal="true">
<persistence strategy="localTempSwap" />
</cache>
控制台输出
ID :1Hibernate:
select
employee0_.empid as empid1_0_0_,
employee0_.empmobile as empmobile2_0_0_,
employee0_.empname as empname3_0_0_,
employee0_.empsalary as empsalary4_0_0_
from
employee2 employee0_
where
employee0_.empid=?
Name :Nguyen VQ, Phone :948380166Salary :6.2
ID :1, Name :Nguyen VQ, Phone :948380166Salary :6.2
ID :1Hibernate:
select
employee0_.empid as empid1_0_0_,
employee0_.empmobile as empmobile2_0_0_,
employee0_.empname as empname3_0_0_,
employee0_.empsalary as empsalary4_0_0_
from
employee2 employee0_
where
employee0_.empid=?
Name :Nguyen VQ, Phone :948380166Salary :6.2
ID4: 1,Name4 :Nguyen VQ,Mobile: 948380166, Salary: 6.2
当我通过 s.evict(e) 删除 e 时;然后是 SessionFactory,但它不工作(它创建新查询)
二级配置似乎设置正确。我怀疑的一件事是 @Cache
属性。您是否使用 @Cache
注释在 Employee 对象上启用了 CacheConcurrencyStrategy。
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE | NONSTRICT_READ_WRITE | TRANSACTIONAL)