views:

110

answers:

3

Hi guys,

I would like to know how to deal with nested transactions e.g. Between one begin and comit, I have another begin and comit. The reason I am asking you is because in my ApplicationServices project I have services that depends on other services. And a method of a parent service begins a transaction and depending upon some logic, it might have to call one of the methods of the dependent child services, that also in turn perform begin and commit. The Child services method are also being used independently as well directy from the controllers, therefore I have to use begin and commit in the child services.

So in short, basically I will end up having somethign like this

   using( Repository1.DbContext.BeginTransaction() )
   {
      try
      {
         .....
         .....
         using( Repository2.DbContext.BeginTransaction() )
         {
            try
            {
               .....
               .....
               Repository2.DbContext.CommitTransaction()
            }
            catch
            {
               Repository2.DbContext.RollBack();
               throw;
            }
        }
        Repository1.DbContext.CommitTransaction()
      }
      catch
      {
         Repository1.DbContext.RollBack();
         throw;
      }
   }

So there are nested begins and commits. I would like to know

-what would be the behaviour, when the nested Repository2 gets committed successfully but the parent Repository1 is rollbacked ?

How can I control this behaviour in code e.g.

-if I don't want to make child transaction part of the parent transaction. -how to figure out wether a transaction is already running before creating a child transaction

or if there is another elegant solution to this problem?

Thanks Nabeel

+3  A: 

NHibernate does not support nested transactions. Is the DbContext is the same for both Repository1 and Repository2? If that's the case then they can participate in the same transaction. If not, you'll have to split the transactions into two methods or look at the System.Transactions namespace.

Jamie Ide
Yes the DBContect is the same for both repositories and after profiling in NHibernate Profiler, it turns out that nhibernate does not begin a new transaction for inner repository.And yes splitting the transaction into two methods, thats exactly what I am doing it at the moment. But I thought there might be a better way of doing this. I have used TransactionScope in the past, so I am thinking to try that as I am using a single SQL 2005 database in my application.
nabeelfarid
I found this another post on SO to be useful as well. http://stackoverflow.com/questions/646318/nhibernate-with-transactionscope
nabeelfarid
A: 

If you place your transaction boundaries at your controllers level then I would say you don't need nested transactions.

Simone
well there is a winform client as well. And I am thinking to keep the transaction management code inside my ApplicationServices project instead of Controllers project, that way I won't have to manage the transaction manipulation code in Winform again.
nabeelfarid
A: 

Have you tried the UnitOfWork attribute in the Sharp-Architecture-Contrib project?

http://github.com/codai/Sharp-Architecture-Contrib

http://tomcabanski.spaces.live.com/blog/cns!E0D3617496209F45!220.entry

http://wiki.sharparchitecture.net/ContribConfiguration.ashx

http://wiki.sharparchitecture.net/ContribUnitOfWork.ashx

Dan
Interesting! I will look into contrib. Thanks Dan
nabeelfarid