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

3
import java.io.Serializable;4

5
import org.hibernate.Session;6

7
import junit.framework.TestCase;8

9

public class CacheLevel1Test extends TestCase
{10

11

/** *//**12
* 在同一个session中发出两次load查询13
*/14

public void testCache1()
{15
Session session = null;16

try
{17
session = HibernateUtils.getSession();18
session.beginTransaction();19
20
Student student = (Student)session.load(Student.class, 1);21
System.out.println("student.name=" + student.getName());22
23
//不会发出sql,因为load使用缓存24
student = (Student)session.load(Student.class, 1);25
System.out.println("student.name=" + student.getName());26
27
session.getTransaction().commit();28

}catch(Exception e)
{29
e.printStackTrace();30
session.getTransaction().rollback();31

}finally
{32
HibernateUtils.closeSession(session);33
}34
} 35

36

/** *//**37
* 在同一个session中发出两次get查询38
*/39

public void testCache2()
{40
Session session = null;41

try
{42
session = HibernateUtils.getSession();43
session.beginTransaction();44
45
Student student = (Student)session.get(Student.class, 1);46
System.out.println("student.name=" + student.getName());47
48
//不会发出sql,因为get使用缓存49
student = (Student)session.get(Student.class, 1);50
System.out.println("student.name=" + student.getName());51
52
session.getTransaction().commit();53

}catch(Exception e)
{54
e.printStackTrace();55
session.getTransaction().rollback();56

}finally
{57
HibernateUtils.closeSession(session);58
}59
} 60
61

/** *//**62
* 在同一个session中发出两次iterate查询实体对象63
*/64

public void testCache3()
{65
Session session = null;66

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();78

}catch(Exception e)
{79
e.printStackTrace();80
session.getTransaction().rollback();81

}finally
{82
HibernateUtils.closeSession(session);83
}84
} 85
86

/** *//**87
* 在同一个session中发出两次iterate查询实体对象88
*/89

public void testCache4()
{90
Session session = null;91

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查询普通属性,一级缓存不会缓存,所以发出sql99
//一级缓存是缓存实体对象的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();104

}catch(Exception e)
{105
e.printStackTrace();106
session.getTransaction().rollback();107

}finally
{108
HibernateUtils.closeSession(session);109
}110
} 111
112

/** *//**113
* 开启两个session中发出load查询114
*/115

public void testCache5()
{116
Session session = null;117

try
{118
session = HibernateUtils.getSession();119
session.beginTransaction();120
121
Student student = (Student)session.load(Student.class, 1);122
System.out.println("student.name=" + student.getName());123

124
session.getTransaction().commit();125

}catch(Exception e)
{126
e.printStackTrace();127
session.getTransaction().rollback();128

}finally
{129
HibernateUtils.closeSession(session);130
}131
132

try
{133
session = HibernateUtils.getSession();134
session.beginTransaction();135
136
//会发出查询语句,session间不能共享一级缓存的数据137
//因为它会伴随session的生命周期存在和消亡138
Student student = (Student)session.load(Student.class, 1);139
System.out.println("student.name=" + student.getName());140

141
session.getTransaction().commit();142

}catch(Exception e)
{143
e.printStackTrace();144
session.getTransaction().rollback();145

}finally
{146
HibernateUtils.closeSession(session);147
}148
149
} 150
151

/** *//**152
* 在同一个session中先save,在发出load查询save过的数据153
*/154

public void testCache6()
{155
Session session = null;156

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();170

}catch(Exception e)
{171
e.printStackTrace();172
session.getTransaction().rollback();173

}finally
{174
HibernateUtils.closeSession(session);175
}176
} 177

178

/** *//**179
* 向数据库中批量加入1000条数据180
*/181

public void testCache7()
{182
Session session = null;183

try
{184
session = HibernateUtils.getSession();185
session.beginTransaction();186
187

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
//同时清除缓存,避免大量数据造成内存溢出193

if ( i % 20 == 0)
{194
session.flush();195
session.clear();196
}197
}198
199
session.getTransaction().commit();200

}catch(Exception e)
{201
e.printStackTrace();202
session.getTransaction().rollback();203

}finally
{204
HibernateUtils.closeSession(session);205
}206
} 207
208
}209

posted on
浙公网安备 33010602011771号