Hibernate 缓存 之 Session 实现(一)
来源:互联网 发布:足球黑色三分钟 知乎 编辑:程序博客网 时间:2024/06/05 10:41
首先以查询学生为例,模拟一个缓存的机制
public class Test { public static void main(String[] args) throws Exception { MyClassDao myClassDao = new MyClassDao(); StudentDao studentDao = new StudentDao(); Student student1 = studentDao.findById("4028810027d8be080127d8be0d790002"); System.out.println(student1.getName()); Student student2 = studentDao.findById("4028810027d8be080127d8be0d790002"); System.out.println(student2.getName()); }}很明显的看到执行了两条SQL语句,感觉有点浪费资源,如果我们把第一次查询的结果保存起来,当第二次查询的时候,先看保存了没有,如果有,直接用,如果没有,则再查询数据库.这样就更好一些.
修改StudentDao层,给Dao类增加一个模拟的缓存集合
public class StudentDao { public static Map<String, Student> cache = new HashMap<String, Student>(); public void create(Student student) throws Exception { Session session = null; Transaction transaction = null; try{ session = HibernateUtil.getSession(); transaction = session.getTransaction(); transaction.begin(); session.save(student); transaction.commit(); }catch (Exception e){ transaction.rollback(); throw e; } } public void delete(Student student) throws Exception { Session session = null; Transaction transaction = null; try{ session = HibernateUtil.getSession(); transaction = session.getTransaction(); transaction.begin(); session.delete(student); transaction.commit(); }catch (Exception e) { transaction.rollback(); throw e; } } public Student findById(Serializable id) throws Exception { Session session = null; Transaction transaction = null; Student student = null; // 如果 缓存中有 先使用缓存的结果 返回输出 String key = Student.class.getName() + id; student = cache.get(key); if (student != null) { return student; } try { session = HibernateUtil.getSession(); transaction = session.getTransaction(); transaction.begin(); student = (Student) session.get(Student.class, id); cache.put(key, student); transaction.commit(); } catch (Exception e) { transaction.rollback(); throw e; } return student; }}再一次执行Test 的 main方法。
只执行了一次查询,这就是缓存的一个作用,但是缓存机制远远不会这么简单,想一下,如果在两次查询之间,对象更新了怎么办,这就涉及到缓存的更新.我们暂时只需要了解一下缓存的机制与简单的实现.因为这一块已经有现成的包和类直接用,不需要我们再从头开发.
缓存的作用主要是用来提高性能,可以简单的理解成一个Map,使用缓存涉及到三个操作:
把数据放入缓存
从缓存中获取数据
删除缓存中无效数据
一级缓存: Session 级共享,此缓存只能在 Session 关闭之前使用.
save, update, saveOrUpdate, get, list, iterate, lock 这些方法都会将对象放在一级缓存中, 一级缓存不能控制缓存的数量, 所以,要注意大批量操作数据时可能造成内在溢出。可以用evict, clear方法清除缓存中的内容.后面 Hibernate 批量增加数据时再演示。
看下面的代码:
public Student findById(Serializable id) throws Exception { Session session = null; Transaction transaction = null; Student student = null; try { session = HibernateUtil.getSession(); transaction = session.getTransaction(); transaction.begin(); student0 = (Student0) session.get(Student.class, id); System.out.println(student0.getName()); Student student1 = (Student) session.get(Student.class, id); System.out.println(student1.getName()); transaction.commit(); } catch (Exception e) { transaction.rollback(); throw e; } return student0;}可以看到上面的代码中,只查询了一次数据库,但是缓存的作用域太小,没有太大的作用.
0 0
- Hibernate 缓存 之 Session 实现(一)
- Hibernate 缓存 之 Session 实现(二)
- hibernate之session缓存
- hibernate学习笔记(一)之Junit 与 Session 一级缓存
- Hibernate学习笔记之session缓存(一级缓存)
- hibernate之Session的缓存
- hibernate优化(一) 之session优化
- Hibernate之二 一级缓存(session)、二级缓存(sessionFactory)
- hibernate框架笔记之session缓存
- Hibernate入门(三)之hibernate 的session的缓存机制
- hibernate一级缓存(session缓存)
- Hibernate Session缓存经验总结
- Hibernate Session缓存概述
- Hibernate Session缓存概述
- hibernate session缓存机制
- Hibernate session的缓存
- hibernate session缓存
- hibernate session缓存机制
- android-多分辨率适配的思路
- python3.5入门1--运行报错:IndentationError: expected an indented block
- Ansoft HFSS____百度百科
- 删除链表中重复的结点
- 控制webview使用html5的video播放视频不全屏(inline)的方法
- Hibernate 缓存 之 Session 实现(一)
- 小萝莉五子棋(高能绕行)
- 社会变革、新医改与医疗信息化
- 人生虽然很艰难,但所有的付出都有回报
- Given a positive integer N, you should output the most right digit of N^N.
- cocos-js,内存管理1---引用计数方式
- ElasticSearch性能测试
- 剑指Offer学习笔记(一)
- 元素类型为 "package" 的内容必须匹配 "(result-types?,interceptors?,default-interceptor-ref?,default-action-ref?..