NHibernate 中事务的使用方法
public virtual int InsertEmployeeInfoByImport(EmployeeInfo info, PersonInfo person, CorporationSaveCountInfo count) { using (ISession session = OpenSession()) { ITransaction tx = null;//创建一个事务 try { tx = session.BeginTransaction();//ISession开始事务 //事务中处理的一系列业务 info.ID = (int)session.Save(info); //回写明细档 person.EmpID = info.ID; person.WorkCode = info.WorkCode; person.ID = (int)session.Save(person); info.PersonID = person.ID; session.Update(info); //UpdateEmployeeInfo(info); //UpdatePerson(person); session.Update(count); tx.Commit();//提交事务 } catch (Exception ex) { if (tx != null) tx.Rollback();//发生异常,回滚事务,保证了数据的一致性 throw ex; } return info.ID; } }
public virtual int InsertEmployeeInfoByImport(EmployeeInfo info, PersonInfo person, CorporationSaveCountInfo count) { using (ISession session = OpenSession()) { ITransaction tx = null; try { tx = session.BeginTransaction(); info.ID = (int)session.Save(info); //回写明细档 person.EmpID = info.ID; person.WorkCode = info.WorkCode; person.ID = (int)session.Save(person); info.PersonID = person.ID; session.Update(info); //UpdateEmployeeInfo(info); //UpdatePerson(person); session.Update(count); tx.Commit(); } catch (Exception ex) { if (tx != null) tx.Rollback(); throw ex; } return info.ID; } }