好的,所以Ayende recommends always using a transaction,即使是阅读操作也是如此 但假设我有以下情况:
public Employee GetEmployeeByName(string name)
{
using (ITransaction tx = CurrentSession.BeginTransaction())
{
return dao.GetEmployeeByName(name);
}
}
public void SaveNewEmployee(Employee employee)
{
using (ITransaction tx = CurrentSession.BeginTransaction())
{
if (GetEmployeeByName(employee.Name) != null)
{
throw new ArgumentException("employee with same name found");
}
CurrentSession.Save(employee);
}
}
这实际上会引发异常,因为nhibernate doesn't support nested transactions 我怎么能绕过这个?
修改
this甚至比我接受的解决方案更好......
答案 0 :(得分:2)
通常情况下,您可以使用工作单元模式解决问题,您可以在打开会话的同时启动交易。也就是说在工作单元的开头。你会在工作单元的最后提交它。