开发者

how to use explicit transactions without nested transactions

ok, so Ayende recommends always using a transaction, even for read operations.

but supposing I have the following scenario:

public Employee GetEmployeeByName(string name)
        {
            using (ITransaction tx = CurrentSession.BeginTransaction())
            {
                return dao.GetEmplo开发者_如何学JAVAyeeByName(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);
        }
    }

this would actually throw an exception, since nhibernate doesn't support nested transactions.

how can I get around this?

EDIT

this is even a better solution than the one I accepted...


Typically you would get around it by using a unit of work pattern in which you can start your transaction at the same time you open your session. That is to say at the beginning of the unit of work. And you would commit it at the end of the unit of work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜