3

I have a TransactionScope() block. It always gets stuck in an insert statement. It appears in the Activity Monitor as a Blocking Task, so it blocks the SQL server, and after the timeout, I get this error:

The operation is not valid for the state of the transaction.

What’s going wrong?

const TransactionScopeOption opt = new TransactionScopeOption();
TimeSpan span = new TimeSpan(0, 0, 1, 30);

try
{
    using (TransactionScope scope01 = new TransactionScope(opt, span))
    {
        using (var sqlcon = new SqlConnection(sSqlCon))
        {
            //select,insert , update statements
        }
    }
}
catch (Exception ex)
{
}
Bill Brown
  • 135
  • 1
  • 2
  • 7

2 Answers2

2

It probably means it aborted. Did you call transaction complete within transaction bracket?

try
{
    using (TransactionScope scope01 = new TransactionScope(opt, span))
    {
        using (var sqlcon = new SqlConnection(sSqlCon))
        {
            //select,insert , update statements
        }

        scope01.Complete();
    }
}

If it doesn't call the Complete, it will automatically rollback.

MilkTea027
  • 301
  • 1
  • 5
  • 24
0

The transaction might have been timed out. check the maching.config for the default time out

<configuration> 
  <system.transactions>
    <machinesettings maxtimeout="00:30:00" />
  </system.transactions>
</configuration>