none
Exception when using TransactionScope RRS feed

  • Question

  • I got below exception:

     

    ErrorType: System.InvalidOperationException

    ErrorDetail:

    Exception Type: System.InvalidOperationException.

    Message: The transaction associated with the current connection has completed but has not been disposed.  The transaction must be disposed before the connection can be used to execute SQL statements..

    Source: System.Data.

     

    My code like below:

     

    public void WriteRefundTransaction(List<Summary> summary)
    {
    	using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, new TimeSpan(0, 30, 0)))
    	using (SqlConnection conn = new SqlConnection(m_ConnectionString))
    	{
    		conn.Open();
    		m_dal.InsertTransaction(Entity, conn);
    		
    		m_dal.DeleteBufferData(Entity.TransactionNumber, conn);
    		
    		ts.Complete();
    	}
    }
    

     

    The code is called from a Parallel Task. 

    I never got this exception, please help. Thanks!

    Thursday, October 14, 2010 1:40 AM

Answers

  • I think you are having issues due multithreading and using the same transaction scope. In this case I don't think you really need a TransactionScop thus you could use SqlTransaction class instead. Or try with TransactionScopeOptional.RequiresNew option.

    Miha Markic [MVP C#] http://blog.rthand.com
    • Marked as answer by liurong luo Friday, October 22, 2010 3:33 AM
    Thursday, October 14, 2010 10:22 AM

All replies

  • I think you are having issues due multithreading and using the same transaction scope. In this case I don't think you really need a TransactionScop thus you could use SqlTransaction class instead. Or try with TransactionScopeOptional.RequiresNew option.

    Miha Markic [MVP C#] http://blog.rthand.com
    • Marked as answer by liurong luo Friday, October 22, 2010 3:33 AM
    Thursday, October 14, 2010 10:22 AM
  • I will try. But I still can not understand, why different thread will use same transaction scope?
    Thursday, October 14, 2010 11:44 PM