Reputation: 2017
I have multiple operations in the DoWork() Method, where i'm using the transaction scope,but it is not working as expected.
Whenever there is a failure in DataInsert2(), it should revert both DataInsert1() and DataInsert2(). But currently it is reverting only DataInsert2()..?
Let me know if i'm doing any mistakes here.
//DoWork()
public void DoWork()
{
try
{
TransactionOptions tranOptions = UtilityBL.GetTransOptions();
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, tranOptions))
{
if (DataInsert1())
{
DataInsert2() ;
}
scope.Complete();
}
}
catch (Exception ex)
{
log.Info(string.Format(UploadMessages.FailedMsg));
if (ContextUtil.IsInTransaction)
ContextUtil.SetAbort();
}
}
//DataInsert1
public bool DataInsert1()
{
bool fileUploadStatus=false;
try
{
DAL.InsertDetails1()
fileUploadStatus=true;
}
catch (Exception ex)
{
log.Info(string.Format(UploadMessages.FailedMsg));
}
return fileUploadStatus;
}
//DataInsert2
public bool DataInsert2()
{
try
{
DAL.InsertDetails2()
}
catch (Exception ex)
{
log.Info(string.Format(UploadMessages.FailedMsg));
}
}
Upvotes: 0
Views: 1648
Reputation: 8991
You should be able to simplify your code as follows. Your DataInsert
methods are swallowing any exceptions thrown by the DAL.InsertDetails
methods.
public void DoWork()
{
TransactionOptions tranOptions = UtilityBL.GetTransOptions();
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, tranOptions))
{
try {
DAL.InsertDetails1();
DAL.InsertDetails2();
scope.Complete();
}
catch (Exception ex)
{
log.Info(string.Format(UploadMessages.FailedMsg));
if (ContextUtil.IsInTransaction)
ContextUtil.SetAbort();
}
}
}
Upvotes: 1