Hibernate一级缓存

一级缓存很短和session的生命周期一致,一级缓存也叫session级的缓存或事务级缓存

那些方法支持一级缓存:
 * get()
 * load()
 * iterate(查询实体对象)
 
如何管理一级缓存:
 * session.clear(),session.evict()
 
如何避免一次性大量的实体数据入库导致内存溢出
 * 先flush,再clear
 
如果数据量特别大,考虑采用jdbc实现,如果jdbc也不能满足要求可以考虑采用数据本身的特定导入工具 

 

ContractedBlock.gif ExpandedBlockStart.gif 测试code
  1package com.bjsxt.hibernate;
  2
  3import java.io.Serializable;
  4
  5import org.hibernate.Session;
  6
  7import junit.framework.TestCase;
  8
  9ExpandedBlockStart.gifContractedBlock.gifpublic class CacheLevel1Test extends TestCase {
 10
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 12     * 在同一个session中发出两次load查询
 13     */

 14ExpandedSubBlockStart.gifContractedSubBlock.gif    public void testCache1() {
 15        Session session = null;
 16ExpandedSubBlockStart.gifContractedSubBlock.gif        try {
 17            session = HibernateUtils.getSession();
 18            session.beginTransaction();
 19            
 20            Student student = (Student)session.load(Student.class1);
 21            System.out.println("student.name=" + student.getName());
 22            
 23            //不会发出sql,因为load使用缓存
 24            student = (Student)session.load(Student.class1);
 25            System.out.println("student.name=" + student.getName());
 26            
 27            session.getTransaction().commit();
 28ExpandedSubBlockStart.gifContractedSubBlock.gif        }
catch(Exception e) {
 29            e.printStackTrace();
 30            session.getTransaction().rollback();
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        }
finally {
 32            HibernateUtils.closeSession(session);
 33        }

 34    }
        
 35
 36ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 37     * 在同一个session中发出两次get查询
 38     */

 39ExpandedSubBlockStart.gifContractedSubBlock.gif    public void testCache2() {
 40        Session session = null;
 41ExpandedSubBlockStart.gifContractedSubBlock.gif        try {
 42            session = HibernateUtils.getSession();
 43            session.beginTransaction();
 44            
 45            Student student = (Student)session.get(Student.class1);
 46            System.out.println("student.name=" + student.getName());
 47            
 48            //不会发出sql,因为get使用缓存
 49            student = (Student)session.get(Student.class1);
 50            System.out.println("student.name=" + student.getName());
 51            
 52            session.getTransaction().commit();
 53ExpandedSubBlockStart.gifContractedSubBlock.gif        }
catch(Exception e) {
 54            e.printStackTrace();
 55            session.getTransaction().rollback();
 56ExpandedSubBlockStart.gifContractedSubBlock.gif        }
finally {
 57            HibernateUtils.closeSession(session);
 58        }

 59    }
    
 60    
 61ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 62     * 在同一个session中发出两次iterate查询实体对象
 63     */

 64ExpandedSubBlockStart.gifContractedSubBlock.gif    public void testCache3() {
 65        Session session = null;
 66ExpandedSubBlockStart.gifContractedSubBlock.gif        try {
 67            session = HibernateUtils.getSession();
 68            session.beginTransaction();
 69            
 70            Student student = (Student)session.createQuery("from Student s where s.id=1").iterate().next();
 71            System.out.println("student.name=" + student.getName());
 72            
 73            //会发出查询id的sql,不会发出查询实体对象的sql,因为iterate使用缓存
 74            student = (Student)session.createQuery("from Student s where s.id=1").iterate().next();
 75            System.out.println("student.name=" + student.getName());
 76            
 77            session.getTransaction().commit();
 78ExpandedSubBlockStart.gifContractedSubBlock.gif        }
catch(Exception e) {
 79            e.printStackTrace();
 80            session.getTransaction().rollback();
 81ExpandedSubBlockStart.gifContractedSubBlock.gif        }
finally {
 82            HibernateUtils.closeSession(session);
 83        }

 84    }
            
 85    
 86ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 87     * 在同一个session中发出两次iterate查询实体对象
 88     */

 89ExpandedSubBlockStart.gifContractedSubBlock.gif    public void testCache4() {
 90        Session session = null;
 91ExpandedSubBlockStart.gifContractedSubBlock.gif        try {
 92            session = HibernateUtils.getSession();
 93            session.beginTransaction();
 94            
 95            String name = (String)session.createQuery("select s.name from Student s where s.id=1").iterate().next();
 96            System.out.println("student.name=" + name);
 97            
 98            //iterate查询普通属性,一级缓存不会缓存,所以发出sql
 99            //一级缓存是缓存实体对象的
100            name = (String)session.createQuery("select s.name from Student s where s.id=1").iterate().next();
101            System.out.println("student.name=" + name);
102            
103            session.getTransaction().commit();
104ExpandedSubBlockStart.gifContractedSubBlock.gif        }
catch(Exception e) {
105            e.printStackTrace();
106            session.getTransaction().rollback();
107ExpandedSubBlockStart.gifContractedSubBlock.gif        }
finally {
108            HibernateUtils.closeSession(session);
109        }

110    }
            
111    
112ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
113     * 开启两个session中发出load查询
114     */

115ExpandedSubBlockStart.gifContractedSubBlock.gif    public void testCache5() {
116        Session session = null;
117ExpandedSubBlockStart.gifContractedSubBlock.gif        try {
118            session = HibernateUtils.getSession();
119            session.beginTransaction();
120            
121            Student student = (Student)session.load(Student.class1);
122            System.out.println("student.name=" + student.getName());
123
124            session.getTransaction().commit();
125ExpandedSubBlockStart.gifContractedSubBlock.gif        }
catch(Exception e) {
126            e.printStackTrace();
127            session.getTransaction().rollback();
128ExpandedSubBlockStart.gifContractedSubBlock.gif        }
finally {
129            HibernateUtils.closeSession(session);
130        }

131        
132ExpandedSubBlockStart.gifContractedSubBlock.gif        try {
133            session = HibernateUtils.getSession();
134            session.beginTransaction();
135            
136            //会发出查询语句,session间不能共享一级缓存的数据
137            //因为它会伴随session的生命周期存在和消亡
138            Student student = (Student)session.load(Student.class1);
139            System.out.println("student.name=" + student.getName());
140
141            session.getTransaction().commit();
142ExpandedSubBlockStart.gifContractedSubBlock.gif        }
catch(Exception e) {
143            e.printStackTrace();
144            session.getTransaction().rollback();
145ExpandedSubBlockStart.gifContractedSubBlock.gif        }
finally {
146            HibernateUtils.closeSession(session);
147        }

148        
149    }
        
150    
151ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
152     * 在同一个session中先save,在发出load查询save过的数据
153     */

154ExpandedSubBlockStart.gifContractedSubBlock.gif    public void testCache6() {
155        Session session = null;
156ExpandedSubBlockStart.gifContractedSubBlock.gif        try {
157            session = HibernateUtils.getSession();
158            session.beginTransaction();
159            
160            Student stu = new Student();
161            stu.setName("王五");
162            
163            Serializable id = session.save(stu);
164            
165            //不会发出sql,因为save是使用缓存的
166            Student student = (Student)session.load(Student.class, id);
167            System.out.println("student.name=" + student.getName());
168            
169            session.getTransaction().commit();
170ExpandedSubBlockStart.gifContractedSubBlock.gif        }
catch(Exception e) {
171            e.printStackTrace();
172            session.getTransaction().rollback();
173ExpandedSubBlockStart.gifContractedSubBlock.gif        }
finally {
174            HibernateUtils.closeSession(session);
175        }

176    }
    
177
178ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
179     * 向数据库中批量加入1000条数据
180     */

181ExpandedSubBlockStart.gifContractedSubBlock.gif    public void testCache7() {
182        Session session = null;
183ExpandedSubBlockStart.gifContractedSubBlock.gif        try {
184            session = HibernateUtils.getSession();
185            session.beginTransaction();
186            
187ExpandedSubBlockStart.gifContractedSubBlock.gif            for (int i=0; i<1000; i++{
188                Student student = new Student();
189                student.setName("s_" + i);
190                session.save(student);
191                //每20条数据就强制session将数据持久化
192                //同时清除缓存,避免大量数据造成内存溢出
193ExpandedSubBlockStart.gifContractedSubBlock.gif                if ( i % 20 == 0{
194                    session.flush();
195                    session.clear();
196                }

197            }

198            
199            session.getTransaction().commit();
200ExpandedSubBlockStart.gifContractedSubBlock.gif        }
catch(Exception e) {
201            e.printStackTrace();
202            session.getTransaction().rollback();
203ExpandedSubBlockStart.gifContractedSubBlock.gif        }
finally {
204            HibernateUtils.closeSession(session);
205        }

206    }
    
207    
208}

209

转载于:https://www.cnblogs.com/ksyou/archive/2009/04/02/1428028.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值