Hibernate中"二级缓存"配置
实体类 :
package cn.happy.entity;
public class Emp {
private Integer empNo;
private String empName;
public Integer getEmpNo() {
return empNo;
}
public void setEmpNo(Integer empNo) {
this.empNo = empNo;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
}
工具类:
1 package cn.happy.util;
2
3 import org.hibernate.Session;
4 import org.hibernate.SessionFactory;
5 import org.hibernate.cfg.Configuration;
6
7 public class HibernateUtil {
8 private static final ThreadLocal sessionTL=new ThreadLocal();
9 private static Configuration cf;
10 private static final SessionFactory factory;
11 static{
12 try {
13 cf=new Configuration().configure();
14 factory=cf.buildSessionFactory();
15 } catch (Exception e) {
16 throw new ExceptionInInitializerError(e);
17 }
18 }
19 public static Session getSession()
20 {
21 //sessionTL的get()方法根据当前线程返回其对应的线程内部变量,
22 //也就是我们需要的Session,多线程情况下共享数据库连接是不安全的。
23 //ThreadLocal保证了每个线程都有自己的Session。
24 Session session = (Session)sessionTL.get();
25 //如果session为null,则打开一个新的session
26 if (session==null) {
27 //创建一个数据库连接对象session
28 session=factory.openSession();
29 //保存该数据库连接session到ThreadLocal中。
30 sessionTL.set(session);
31
32 }
33 //如果当前线程已经访问过数据库了,
34 //则从sessionTL中get()就可以获取该线程上次获取过的数据库连接对象。
35 return session;
36 }
37 /**
38 * 关闭Session
39 */
40 public static void closeSession()
41 {
42 Session session =(Session)sessionTL.get();
43 sessionTL.set(null);
44 session.close();
45 }
46
47 }
测试类:
package cn.happy.test; import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test; import cn.happy.entity.Emp;
import cn.happy.util.HibernateUtil; public class STest {
Transaction tx;
Session session;
Transaction tx2;
Session session2;
@Test
public void testBulk(){
session = HibernateUtil.getSession();
tx=session.beginTransaction();
Emp emp = (Emp)session.get(Emp.class, 1);
System.out.println(emp);
tx.commit();
HibernateUtil.closeSession();
System.out.println("===================");
session2 = HibernateUtil.getSession();
tx2=session2.beginTransaction();
Emp emp2 = (Emp)session2.get(Emp.class, 1);
System.out.println(emp2);
tx2.commit();
HibernateUtil.closeSession();
}
}
小配置:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.entity">
<class name="Emp" table="Emp">
<cache usage="read-write"/>
<id name="empNo" type="int" column="EMPNO">
<generator class="native">
</generator>
</id>
<property name="empName" type="string" column="EMPNAME"/>
</class>
</hibernate-mapping>
大配置:
<?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="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">zc</property>
<property name="connection.password">zc</property>
<!-- 输出所有 SQL 语句到控制台。 -->
<property name="hibernate.show_sql">true</property>
<!-- 配置Hibernate.cfg.xml开启二级缓存。 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!-- 配置二级缓存的供应商 -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <!-- 在 log 和 console 中打印出更漂亮的 SQL。 -->
<property name="hibernate.format_sql">true</property>
<!-- 自动生成数据表,update/create -->
<property name="hbm2ddl.auto">update</property>
<!-- 方言 -->
<property name="hibernate.dialect"> org.hibernate.dialect.Oracle10gDialect</property>
<!-- 关联小配置 --> <mapping resource="cn/happy/entity/Emp.hbm.xml"/>
<class-cache usage="read-write" class="cn.happy.entity.Emp"/>
</session-factory>
</hibernate-configuration>
Jar包导入:
package cn.happy.entity;
public class Emp {
private Integer empNo;
private String empName;
public Integer getEmpNo() {return empNo;}
public void setEmpNo(Integer empNo) {this.empNo = empNo;}
public String getEmpName() {return empName;}
public void setEmpName(String empName) {this.empName = empName;}
}
Hibernate中"二级缓存"配置的更多相关文章
- Hibernate中二级缓存指的是什么?
一.一级缓存.二级缓存的概念解释 (1)一级缓存就是Session级别的缓存,一个Session做了一个查询操作,它会把这个操作的结果放在一级缓存中,如果短时间内这个 session(一定要同一个se ...
- 【Hibernate】解析hibernate中的缓存
Hibernate中的缓存一共有三种,一级缓存.二级缓存.查询缓存.缓存除了使用Hibernate自带的缓存,还可以使用redis进行缓存,或是MongoDB进行缓存. 所使用的Demo: User. ...
- 配置Hibernate的二级缓存
1.在applicationContex.xml文件里面添加二级缓存配置: <!-- 配置hibernate的sessionFactory --> <bean id="se ...
- Hibernate二级缓存简述及基于Spring4,Hibernate5,Ehcache3的二级缓存配置
Hibernate L2缓存 缓存的分类 L2缓存工作原理 放入二级缓存的数据 Ehcache 依赖 ehcache.xml 常用的memoryStoreEvictionPolicy(缓存算法) eh ...
- SSM-MyBatis-18:Mybatis中二级缓存和第三方Ehcache配置
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 二级缓存 Mybatis中,默认二级缓存是开启的.可以关闭. 一级缓存开启的.可以被卸载吗?不可以的.一级缓存 ...
- Hibernate 二级缓存配置
详见:https://www.cnblogs.com/Junsept/p/7324981.html Hibernate的cache管理: Cache就是缓存,它往往是提高系统性能的最重要手段,对数据起 ...
- Hibernate中一级缓存和二级缓存
缓存是介于应用程序和物理数据源之间,其作用是为了降低应用程序对物理数据源访问的频次,从而提高了应用的运行性能.缓存内的数据是对物理数据源中的数据的复制,应用程序在运行时从缓存读写数据,在特定的时刻或事 ...
- Hibernate之二级缓存
Hibernate之二级缓存 一.简介 Gaving King曾经对别人说,hibern ...
- hibernate(九) 二级缓存和事务级别详讲
序言 这算是hibernate的最后一篇文章了,下一系列会讲解Struts2的东西,然后说完Struts2,在到Spring,然后在写一个SSH如何整合的案例.之后就会在去讲SSM,在之后我自己的个人 ...
随机推荐
- Python中的None与Null(空字符)的区别
参考自 Python中的None与空字符(NULL)的区别 - CSDN博客 http://blog.csdn.net/crisschan/article/details/70312764 首先了解p ...
- 磁盘结构,平均寻道时间,平均延迟时间,虚拟内存与MMU
首先了解一下磁盘:磁盘低速的原因是因为它一种机械装置,在磁盘中有一个或多个金属盘片,它们以5400,7200或10800rpm(RPM =revolutions per minute 每分钟多少转 ) ...
- jquery模拟点击A标签的问题
我尝试过多次用jQuery模拟用户点击a标签的功能,但都没有成功,并且困扰了很久. 先看下边的代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <htm ...
- CDH5离线安装简记
需要的介质如下:CM: cloudera-manager-el6-cm5.4.3_x86_64.tar.gzCDH parcel: CDH-5.4.0-1.cdh5.4.0.p0.27-el6.par ...
- C# DateTime 获取时间方法,网上收集
DateTime dt = DateTime.Now; //当前时间 DateTime startWeek = dt.AddDays( - Convert.ToInt32(dt.DayOfWeek.T ...
- svn不提交.net项目中的bin
1 选中 bin->右击->tortoiseSVN->add to ignore list->选择第二个 2 提交 , 服务器上就没有bin目录了.
- SQL执行并返回执行前/后结果
SQLServer: 1.插入数据,并返回插入的数据: INSERT INTO TestTB(Province,City) output inserted.Province, inserted.Cit ...
- JS对象深入剖析
对象概述 Objects are mutable keyed collections. An object is a container of properties, where a propert ...
- centos7安装kvm环境采用网桥模式并创建虚拟机制作openstack需要的镜像
初始环境的安装:centos7 mini iso镜像进行安装的系统 采用的环境是vm该软件,联网方式NAT模式下配置的静态ip(如何在NAT模式下配置静态ip参考之前的文章) 1.由于要安装kvm环境 ...
- 再也不学AJAX了!(二)使用AJAX
在上一篇文章中我们知道,AJAX是一系列技术的统称.在本篇中我们将更进一步,详细解释如何使用Ajax技术在项目中获取数据.而为了解释清楚,我们首先要搞清楚我们是从哪里获取数据的,其次我们关注的才是获取 ...