hibernate采用缓存机制存储数据,如果大量数据提交时 缓存不够就会报错,所以在处理批量插入数据时
要定期的清空hibernate缓存。
例如以下代码:
1- 采取分批提交策略。
2- 如果不写commit的话默认事务最后自动提交,也就是只提交一次(第二种写法)
3- 可以直接调用JDBC addBatch方法进行处理;
第一种写法
public void addBatchChannelFundRelation(Tchannelfundrelation vpo,int currentCount)
throws DAOException {
try {
Session hs = HibernateUtil.currentSession();
Transaction tr = hs.beginTransaction();
hs.save(vpo);
hs.flush();
if (currentCount%50==0) { //每五十条记录提交一次
tr.commit();
hs.clear(); //清理SESSION内存
}
} catch (HibernateException he) {
throw new DAOException (he.getMessage(),he);
}
第二种写法
也可以:最后一次提交
public void addBatchChannelFundRelation(Tchannelfundrelation vpo,int currentCount)
throws DAOException {
try {
Session hs = HibernateUtil.currentSession();
Transaction tr = hs.beginTransaction();
hs.save(vpo);
if (currentCount%50==0) { //每五十条记录提交一次
hs.flush();
hs.clear(); //清理SESSION内存
}
} catch (HibernateException he) {
throw new DAOException (he.getMessage(),he);
}