一级缓存

查询两次id为1的user

User user1 = entityManager.find(User.class, 1);
User user2 = entityManager.find(User.class, 1);

结果发现仅仅调用了一次sql查询,由于使用了一级缓存

假设查询一次后,关掉entityManager,再查询

User user1 = entityManager.find(User.class, 1);

entityManager.close();
entityManager = factory.createEntityManager(); User user2 = entityManager.find(User.class, 1);

发现查询了两次。由于entityManager关闭之后,缓存也就没有了。

使用二级缓存

所谓的二级缓存,也就是能够跨entityManager的缓存,也就是说:就算你关闭了entityManager,缓存也依旧在。

在配置文件persistence.xml中配置

<!-- 二级缓存相关 -->
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>
<property name="hibernate.cache.use_query_cache" value="true"/>

缓存须要下面jar包:

在src下增加一个配置文件:ehcache.xml。这个文件直接拷贝来用即可了,不用理会里面的内容。有须要的时候再研究也不迟

<ehcache>

    <!-- Sets the path to the directory where cache .data files are created.

         If the path is a Java System Property it is replaced by
its value in the running VM. The following properties are translated:
user.home - User's home directory
user.dir - User's current working directory
java.io.tmpdir - Default temp file path -->
<diskStore path="java.io.tmpdir"/> <!--Default Cache configuration. These will applied to caches programmatically created through
the CacheManager. The following attributes are required for defaultCache: maxInMemory - Sets the maximum number of objects that will be created in memory
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
is never expired.
timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
if the element is not eternal. Idle time is now - last accessed time
timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
if the element is not eternal. TTL is now - creation time
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
has reached the maxInMemory limit. -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/> <!--Predefined caches. Add your cache configuration settings here.
If you do not have a configuration for your cache a WARNING will be issued when the
CacheManager starts The following attributes are required for defaultCache: name - Sets the name of the cache. This is used to identify the cache. It must be unique.
maxInMemory - Sets the maximum number of objects that will be created in memory
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
is never expired.
timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
if the element is not eternal. Idle time is now - last accessed time
timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
if the element is not eternal. TTL is now - creation time
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
has reached the maxInMemory limit. --> <!-- Sample cache named sampleCache1
This cache contains a maximum in memory of 10000 elements, and will expire
an element if it is idle for more than 5 minutes and lives for more than
10 minutes. If there are more than 10000 elements it will overflow to the
disk cache, which in this configuration will go to wherever java.io.tmp is
defined on your system. On a standard Linux system this will be /tmp"
-->
<cache name="sampleCache1"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/> <!-- Sample cache named sampleCache2
This cache contains 1000 elements. Elements will always be held in memory.
They are not expired. -->
<cache name="sampleCache2"
maxElementsInMemory="1000"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/> --> <!-- Place configuration for your caches following --> </ehcache>

启用二级缓存:

1.在实体类上加注解@Cacheable(true)

@Cacheable(true)
@Table(name="T_USER")
@Entity
public class User ...

2.在配置文件persistence.xml中配置二级缓存的策略

<!--
配置二级缓存的策略
ALL:全部的实体类都被缓存
NONE:全部的实体类都不被缓存.
ENABLE_SELECTIVE:标识 @Cacheable(true) 注解的实体类将被缓存
DISABLE_SELECTIVE:缓存除标识 @Cacheable(false) 以外的全部实体类
UNSPECIFIED:默认值。JPA 产品默认值将被使用
-->
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>

注意:这个配置要放在provider 节点和class 节点后面

再次运行

User user1 = entityManager.find(User.class, 1);

entityManager.close();
entityManager = factory.createEntityManager(); User user2 = entityManager.find(User.class, 1);

结果仅仅调用了一次sql查询语句,说明二级缓存 起作用了。

JPA学习笔记(11)——使用二级缓存的更多相关文章

  1. Spring 源码学习笔记11——Spring事务

    Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...

  2. Ext.Net学习笔记11:Ext.Net GridPanel的用法

    Ext.Net学习笔记11:Ext.Net GridPanel的用法 GridPanel是用来显示数据的表格,与ASP.NET中的GridView类似. GridPanel用法 直接看代码: < ...

  3. SQL反模式学习笔记11 限定列的有效值

    目标:限定列的有效值,将一列的有效字段值约束在一个固定的集合中.类似于数据字典. 反模式:在列定义上指定可选值 1. 对某一列定义一个检查约束项,这个约束不允许往列中插入或者更新任何会导致约束失败的值 ...

  4. golang学习笔记11 golang要用jetbrain的golang这个IDE工具开发才好

    golang学习笔记11   golang要用jetbrain的golang这个IDE工具开发才好  jetbrain家的全套ide都很好用,一定要dark背景风格才装B   从File-->s ...

  5. Spring MVC 学习笔记11 —— 后端返回json格式数据

    Spring MVC 学习笔记11 -- 后端返回json格式数据 我们常常听说json数据,首先,什么是json数据,总结起来,有以下几点: 1. JSON的全称是"JavaScript ...

  6. JPA学习笔记(8)——映射一对多关联关系

    一对多关联关系 本文有很多和多对一是一样的,因此不会写得非常具体. 有看不懂的.能够參考JPA学习笔记(7)--映射多对一关联关系 Order实体类 package com.jpa.helloworl ...

  7. Python3+Selenium3+webdriver学习笔记11(cookie处理)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记11(cookie处理)'''from selenium im ...

  8. 微信小程序开发:学习笔记[9]——本地数据缓存

    微信小程序开发:学习笔记[9]——本地数据缓存 快速开始 说明 本地数据缓存是小程序存储在当前设备上硬盘上的数据,本地数据缓存有非常多的用途,我们可以利用本地数据缓存来存储用户在小程序上产生的操作,在 ...

  9. 并发编程学习笔记(11)----FutureTask的使用及实现

    1. Future的使用 Future模式解决的问题是.在实际的运用场景中,可能某一个任务执行起来非常耗时,如果我们线程一直等着该任务执行完成再去执行其他的代码,就会损耗很大的性能,而Future接口 ...

  10. 《C++ Primer Plus》学习笔记11

    <C++ Primer Plus>学习笔记11 第17章 输入.输出和文件 <<<<<<<<<<<<<< ...

随机推荐

  1. SGU 323 Aviamachinations

    Aviamachinations Time Limit: 4500ms Memory Limit: 65536KB This problem will be judged on SGU. Origin ...

  2. 小贝_redis高级应用-安全性

    redis高级应用-安全性 一.为什么redis须要安全性 二.设置redis验证password 三.验证   一.为什么redis须要安全性 1.redis作为数据的存储介质.假设无法保证redi ...

  3. javascript变量类型及作用域

    javascript变量类型及作用域 一.简介 变量类型 ECMAScript变量可能包含两种不同类型的数据值:基本类型和引用类型. 基本类型 基本类型指的是简单的数据段,5种基本数据类型:undef ...

  4. javafx KeyFrame

    import javafx.animation.Animation; import javafx.animation.KeyFrame; import javafx.animation.Timelin ...

  5. C#开发 —— 异常处理

    System.ArithmeticException 在算术运行期间发生异常 System.ArrayTypeMismatchException 存储元素的实际类型与数组的实际类型不兼容而导致存储失败 ...

  6. SQL查询结果排序

    <第二章:查询结果排序>1:以指定的次序返回查询结果条件:显示部门10中员工名字,职位和工资并按照工资升序排列:升序asc   降序descSELECT ename,job,sal FRO ...

  7. 在物理 Data Guard 中对异构主备系统的支持 (文档 ID 1602437.1)

    Data Guard中主数据库与物理备用数据库(Redo Apply)之间可以有什么差别?本说明针对重做应用和 Oracle Data Guard 12 发行版 1 进行了更新.它适用于 Oracle ...

  8. MD基本语法介绍

    Markdown基本语法介绍 前言 文本编辑器一般用的有富文本编辑器(也就是Word)和md了,但是wold太过于花里胡哨很多功能都用不上,所以就选择md了,简单实用,一对于我来说一般就用标题和列表就 ...

  9. ps---报告当前系统的进程状态

    ps aux最初用到Unix Style中,而ps -ef被用在System V Style中,两者输出略有不同.现在的大部分Linux系统都是可以同时使用这两种方式的. linux上进程有5种状态: ...

  10. using the easy connect naming method 简单连接測试

    一直都不明确sqlnet.ora中的NAMES.DIRECTORY_PATH= (TNSNAMES, EZCONNECT)是什么意思.今天看到一篇文档,就是登陆选用的方式.做一个測试: tnsname ...