批量删除 哪个好?

zsf0615020117 2009-09-19 03:40:10
方法一:
public static void delMany(String[] delid,HibernateDaoSupport hibernateDAO, Class classname)throwsException {
if(delid==null || hibernateDAO==null || classname==null){
throw new Exception("传值错误");
}
Session session=hibernateDAO.getSessionFactory().getCurrentSession();
session.setFlushMode(FlushMode.AUTO);
Transaction tr=session.beginTransaction();
try{
for(int i=0,n=delid.length;i<n;i++){
Object obj=session.get(classname, delid[i]);
if(obj==null){
throw new Exception("数据库错误");
}
session.delete(obj);
}
session.flush();
tr.commit();
}catch(Exception e){
tr.rollback();
e.printStackTrace();
throw new Exception("删除失败");
}finally{
session.close();
}
}

方法二:
public static void delMany(String[] delid,HibernateDaoSupport hibernateDAO, Class classname)throwsException {
if(delid==null || hibernateDAO==null || classname==null){
throw new Exception("传值错误");
}
Session session=hibernateDAO.getSessionFactory().getCurrentSession();
session.setFlushMode(FlushMode.AUTO);
Transaction tr=session.beginTransaction();
try{
for(int i=0,n=delid.length;i<n;i++){
Object obj=session.get(classname, delid[i]);
if(obj==null){
throw new Exception("数据库错误");
}
session.delete(obj);
if(i%20==0){ [color=#FF0000]//已经配置了hibernate.jdbc.batch_size"值为25
session.flush();
session.clear();
}[/color] }
session.flush();
tr.commit();
}catch(Exception e){
tr.rollback();
e.printStackTrace();
throw new Exception("删除失败");
}finally{
session.close();
}
}

方法二只比方法一多出蓝色部分的代码 到底哪一个使用更方便 更高效呢
...全文
48 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
随风醉舞 2009-10-29
  • 打赏
  • 举报
回复
自己測試一下就知道了!

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧