我有一个TransactionScope()
块。它总是卡在插入语句中。它在活动监视器中显示为“阻止任务”,因此它会阻止SQL Server,并且在超时后,我将收到此错误:
该操作对于事务状态无效。
怎么了?
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)
{
}
这可能意味着它已中止。您是否在交易支架内将交易称为完成?
try
{
using (TransactionScope scope01 = new TransactionScope(opt, span))
{
using (var sqlcon = new SqlConnection(sSqlCon))
{
//select,insert , update statements
}
scope01.Complete();
}
}
如果不调用完成,它将自动回滚。
交易可能已超时。检查maching.config的默认超时时间
<configuration>
<system.transactions>
<machinesettings maxtimeout="00:30:00" />
</system.transactions>
</configuration>