日期:2014-05-17 浏览次数:20681 次
@Override public void addUser(User user) { Session session = null; Transaction trans = null; try { session = HibernateUtil.getSessionFactory().getCurrentSession(); trans = session.beginTransaction(); //保存用户 session.save(user); //制造异常 System.out.println(1/0); //记录日志 LogMgr logMgr = new LogMgrImpl(); Log log = new Log(); log.setOperationTime(new Date()); log.setDetail("保存用户"); log.setUserName(user.getName()); logMgr.addLog(log); trans.commit(); } catch (Exception e) { e.printStackTrace(); trans.rollback(); // TODO: handle exception } }
@Override public void addLog(Log log) { HibernateUtil.getSessionFactory().getCurrentSession().save(log); }
<session-factory> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="hibernate.connection.url"> jdbc:mysql://localhost/SSH2?characterEncoding=UTF-8 </property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">admin</property> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="hibernate.show_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.current_session_context_class">thread</property> <mapping resource="com/spg/bean/User.hbm.xml" /> <mapping resource="com/spg/bean/Log.hbm.xml" /> </session-factory>