如果要将很多对象持久化,你必须通过经常的调用 flush() 以及稍后调用 clear() 来控制第一级缓存的大小。
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
Customer customer = new Customer(.....);
session.save(customer);
if ( i % 20 == 0 ) { //20, same as the JDBC batch size //20,与JDBC批量设置相同
//flush a batch of inserts and release memory:
//将本批插入的对象立即写入数据库并释放内存
session.flush();
session.clear();
}
}
tx.commit();
session.close();
本文介绍了一种在进行大量对象持久化操作时的有效方法:通过控制第一级缓存大小,使用循环调用flush()和clear()的方法来提高效率。具体实现是在每保存一定数量的对象后,调用flush()将当前批次的数据立即写入数据库,并调用clear()释放内存。
&spm=1001.2101.3001.5002&articleId=1909171&d=1&t=3&u=89b660f2852b4ea1b9008b91bc83d786)
1万+

被折叠的 条评论
为什么被折叠?



