none
Nested Transaction with TransactionScopeOption.RequiresNew getting timeout RRS feed

  • Question

  • I have following code path 

    using (var transaction1 = new TransactionScope())
    {
         // code lines.... 
         using (var transaction2 = new TransactionScope(TransactionScopeOption.RequiresNew))
         {
               // code lines. second transaction is to make sure that if parent transaction throw any exception, this transaction must not get affected by it and execute successfully.
               using (var db = new DbContext())
               {
                    db.SaveChanges(); // timeout exception
                    transaction2.Complete();
               }         
         }
         transaction1.Complete();
     }

    Here is the exception stack trace :

    System.Data.Entity.Infrastructure.DbUpdateException : An error occurred while updating the entries. See the inner exception for details.
      ----> System.Data.Entity.Core.UpdateException : An error occurred while updating the entries. See the inner exception for details.
      ----> System.Data.SqlClient.SqlException : Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
    The statement has been terminated.
      ----> System.ComponentModel.Win32Exception : The wait operation timed out
       at System.Data.Entity.Internal.InternalContext.SaveChanges()
       at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()

    FYI, I am using EntityFramework Version 6.1.0.

    As a business logic inside the trasaction is very simple SQL insert statement. any guess why it is taking too much time to complete it ? BTW, It is working fine with Oracle database server.

    Any kind of help is appreciated..!!!

    • Moved by CoolDadTx Thursday, May 11, 2017 3:46 PM EF related
    Thursday, May 11, 2017 1:22 PM

All replies

  • Hi Hiren Bharadwa,

    Based on your description and related code, I create a simple console app demo as below with entity framework6.1.3 and .net framework4.6.1, it works well, could you please share a simple complete demo which could reproduce the issue via OneDrive.

    using System.Transactions;
    
    namespace NestedTransactionDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (var transaction1 = new TransactionScope())
                {
                    // code lines.... 
                    using (var transaction2 = new TransactionScope(TransactionScopeOption.RequiresNew))
                    {
                        // code lines. second transaction is to make sure that if parent transaction throw any exception, this transaction must not get affected by it and execute successfully.
                        using (var db = new MyDbContext())
                        {
                            User u = new User() { UserName = "Test1", FirstName = "Test1" };
                            db.Users.Add(u);
                            db.SaveChanges(); // timeout exception
                            transaction2.Complete();
                        }
                    }
                    transaction1.Complete();
                }
            }
        }
    }
    

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 12, 2017 8:34 AM
    Moderator