J. Ed
J. Ed

Reputation: 6742

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.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);
        }
    }

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...

Upvotes: 0

Views: 136

Answers (1)

Vadim
Vadim

Reputation: 17957

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.

Upvotes: 2

Related Questions