The operation is not valid for the state of the transaction

拥有回忆 提交于 2020-01-02 07:24:11

问题


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)
{
}

回答1:


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.



来源:https://stackoverflow.com/questions/20051527/the-operation-is-not-valid-for-the-state-of-the-transaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!