首先看一段代码
public class UserManagerImpl implements UserManager {
public void addUser(User user) {
Session session = null;
try {
//session = HibernateUtils.getSession();
session = HibernateUtils.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(user);
Log log = new Log();
log.setType("操作日志");
log.setTime(new Date());
log.setDetail("XXX");
LogManager logManager = new LogManagerImpl(); //使用接口实例化,然后又new实现类,以便以后duo
logManager.addLog(log);
Integer.parseInt("adfsadfdsf"); //出现错误了,跑到catch里去执行,然后rollback回滚,
//回滚以后上面向数据库里添加的代码都没用session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
//}finally {
// HibernateUtils.closeSession(session);
}
}
}
注意回滚