aspx.cs 代码: 代码本身相当大,这里的代码是虚构的,但它看起来(重要部分)是这样的:
foreach (Transaction trans in vTransactionList)
{
switch (trans)
{
case 201: codehere; break;
case 202: codehere; break;
case 203:
vProcesso.MarcaEnvioServico(
trans.ProcessId,
trans.CodTrans);
break;
}
}
商务舱方法:
RENDataContext db = new RENDataContext();
Processo update = tabela.SingleOrDefault(
x => x.CodTrans == pCodTrans);
update.SentDate= DateTime.Now;
update.ProcessId = pProcessId;
update.LogUsuario = pUsuario_Id;
update.LogVersaoRegistro = servico.LogVersaoRegistro + 1;
update.LogDataAlteracao = DateTime.Now;
db.SubmitChanges();
有时(非常频繁地,我在执行此代码时收到此错误(sqlserverexception):“服务器无法恢复事务。”同样,它只是随机的,有时会执行,有时不会。当它首先失败时,它会执行在一段时间内持续失败。
我使用存储过程而不是 LINQ 代码来更新表,并且发生了同样的问题。
最佳答案
您可能想查阅此链接: server failed to resume transaction 。我所看到的此错误的摘要是使用单个连接执行多个命令。然后,当根据 SQL Server 已提交或回滚事务时,客户端代码会对事务仍然有效感到困惑。在查看业务类方法的代码时,我注意到上下文不在 using 语句中。上下文类应该实现 IDisposable,因此在使用完上下文后应该调用 Dispose。您可以尝试将代码更改为:
using(RENDataContext db = new RENDataContext()){
Processo update = tabela.SingleOrDefault(
x => x.CodTrans == pCodTrans);
update.SentDate= DateTime.Now;
update.ProcessId = pProcessId;
update.LogUsuario = pUsuario_Id;
update.LogVersaoRegistro = servico.LogVersaoRegistro + 1;
update.LogDataAlteracao = DateTime.Now;
db.SubmitChanges();
}
看看这是否不能解决您的问题。
关于c# - SQL Server 2008 + .NET3.5 + LINQ 中出现 "Server failed to resume the transaction"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3895098/