0

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));
            }
        }
Vishal I P
  • 2,005
  • 4
  • 24
  • 41
  • 1
    Without knowing what's in `DAL`, it's not possible to tell why this might not work. In particular, `TransactionScope` itself doesn't really do anything, code running inside it must be transaction-aware and opt in. This may not happen if (for example) it spans new threads to do work, or if the work you're doing doesn't actually support transactions. – Jeroen Mostert Jan 29 '19 at 12:30
  • 1
    Looks like you're relying on an exception to be caught in `DoWork()` to roll back your transaction but in the data insert methods you are swallowing them. – Chris Pickford Jan 29 '19 at 12:31
  • @ChrisPickford I tried by adding transaction abort() in data insert catch blocks as well. But still the same issue. – Vishal I P Jan 29 '19 at 12:39
  • https://stackoverflow.com/questions/494550/how-does-transactionscope-roll-back-transactions you do not need explicit rollback, so you can even put thy try-catch block outside the transaction scope. It works out of box and you need just Complete(). – Bobo Jan 29 '19 at 12:57

1 Answers1

1

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();
    }
  }
}
Chris Pickford
  • 8,642
  • 5
  • 42
  • 73