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