批量删除 哪个好?
方法一:
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();
}
}
方法二只比方法一多出蓝色部分的代码 到底哪一个使用更方便 更高效呢