public int delete(int sID) {
dbc = new DataBaseConnection();
Connection con = dbc.getConnection();
try {
con.setAutoCommit(false);// 更改JDBC事务的默认提交方式
dbc.executeUpdate("delete from xiao where ID=" + sID);
dbc.executeUpdate("delete from xiao_content where ID=" + sID);
dbc.executeUpdate("delete from xiao_affix where bylawid=" + sID);
con.commit();//提交JDBC事务
con.setAutoCommit(true);// 恢复JDBC事务的默认提交方式
dbc.close();
return 1;
}
catch (Exception exc) {
con.rollBack();//回滚JDBC事务
exc.printStackTrace();
dbc.close();
return -1;
}
}
这是JDBC提交事务的方式,有时间去学习下Hibernate的事务管理机制
try{
Session session = HibernateUtils.getSessionFactory().getCurrentSeesion();
seesion.beginTransation();
session.getTransact().commit();//提交事物
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();//回滚事物
}
getCurrentSession()与openSession()的区别
采用getCurrentSeesion()创建的session会绑定到当前线程中,而采用openSession(),创建的session则
不会
采用getCurrentSession()创建的session在commit或rollback时,会自动关闭,而采用openSession()创
建的session必须手动关闭