Java 冬眠+;简单示例中的ehcache二级缓存未命中
我使用hibernate统计信息来收集关于2级缓存未命中或命中的信息。 为什么在执行测试模块时,只有二级未命中,而没有命中? 为什么在我的示例中二级缓存不起作用 我有下一个hibernate.cfg.xml:Java 冬眠+;简单示例中的ehcache二级缓存未命中,java,hibernate,caching,ehcache,Java,Hibernate,Caching,Ehcache,我使用hibernate统计信息来收集关于2级缓存未命中或命中的信息。 为什么在执行测试模块时,只有二级未命中,而没有命中? 为什么在我的示例中二级缓存不起作用 我有下一个hibernate.cfg.xml: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@192.168.100.0:1521:rrr</property>
<property name="hibernate.connection.username">ora</property>
<property name="hibernate.connection.password">lll</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.default_schema">ora</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.generate_statistics">true</property>
<property name="show_sql">false</property>
<property name="format_sql">false</property>
<property name="use_sql_comments">false</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<property name="net.sf.ehcache.configurationResourceName">/ehcache.xml</property>
<mapping class="com.ric.bill.model.bs.Lst"></mapping>
<mapping class="com.ric.bill.model.bs.LstTp"></mapping>
</session-factory>
</hibernate-configuration>
我的测试模块:
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
SessionFactory sf = HibernateUtil.getSessionFactory();
Session sess = sf.openSession();
sess.beginTransaction();
for (int a=1401; a<=1452; a++) {
Lst lst = (Lst) sess.load(Lst.class, a);
System.out.println(lst.getName());
}
for (int a=1401; a<=1452; a++) {
Lst lst = (Lst) sess.load(Lst.class, a);
System.out.println(lst.getName());
}
Statistics stats = sf.getStatistics();
printStats(stats, 0);
sess.getTransaction().commit();
System.out.println("Complete!");
}
}
二级缓存用于在不同会话之间缓存实体。在这里,您处于同一会话中,因此hibernate不必转到2级缓存:它可以从会话中获取实体 正如你所看到的,你只有52次失误,而不是104次。只有第一个for循环生成这些未命中。然后hibernate获取实体,将它们放在会话和二级缓存中。在第二个循环中,它在会话中找到它们,并且不必查看二级缓存,因此不会生成命中或未命中
要测试2级缓存,请在第一个for循环结束时关闭事务和会话,并在第二个for循环之前打开新的事务和会话。真的!我关闭了第一个会话并打开了另一个会话,它现在可以工作了:获取计数=52第二级命中计数=52第二级未命中计数=52第二级放置计数=52完成!是的,这是第二级缓存的使用。注意:现在您的实体在线程之间共享。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.journaldev.hibernate</groupId>
<artifactId>HibernateEHCacheExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>Hibernate Secondary Level Cache Example using EHCache implementation</description>
<dependencies>
<!-- Hibernate Core API -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.1.0.Final</version>
</dependency>
<!-- EHCache Core APIs -->
<!-- http://mvnrepository.com/artifact/net.sf.ehcache/ehcache-core -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.11</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-ehcache -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.2.0.Final</version>
</dependency>
<!-- EHCache uses slf4j for logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.5</version>
</dependency>
</dependencies>
</project>
package com.ric.bill.model.bs;
import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import com.ric.bill.Simple;
@SuppressWarnings("serial")
@Entity
@Table(name = "LIST", schema="BS")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region="billCache")
@Cacheable
public class Lst implements java.io.Serializable, Simple {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", updatable = false, nullable = false)
private Integer id; //id
@Column(name = "CD", updatable = false, nullable = false)
private String cd; //cd
@Column(name = "NAME", updatable = false, nullable = false)
private String name; //Наименование
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="FK_LISTTP", referencedColumnName="ID")
private LstTp lstTp ;
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCd() {
return this.cd;
}
public void setCd(String cd) {
this.cd = cd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LstTp getLstTp() {
return lstTp;
}
public void setLstTp(LstTp lstTp) {
this.lstTp = lstTp;
}
}
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
SessionFactory sf = HibernateUtil.getSessionFactory();
Session sess = sf.openSession();
sess.beginTransaction();
for (int a=1401; a<=1452; a++) {
Lst lst = (Lst) sess.load(Lst.class, a);
System.out.println(lst.getName());
}
for (int a=1401; a<=1452; a++) {
Lst lst = (Lst) sess.load(Lst.class, a);
System.out.println(lst.getName());
}
Statistics stats = sf.getStatistics();
printStats(stats, 0);
sess.getTransaction().commit();
System.out.println("Complete!");
}
}
Fetch Count=52
Second Level Hit Count=0
Second Level Miss Count=52
Second Level Put Count=52
Complete!