gpt4 book ai didi

sql-server - ZombieCheck 异常 - 此 SqlTransaction 已完成;它不再可用——在简单提交期间

转载 作者:行者123 更新时间:2023-12-02 07:29:25 26 4
gpt4 key购买 nike

我有以下代码,用于将单行提交到数据库表(SQL 2008/.NET 4)

using (var db = new MyDbDataContext(_dbConnectionString))
{
Action action = new Action();
db.Actions.InsertOnSubmit(dbAction);
db.SubmitChanges();
}

通常一切都很好,但偶尔我会遇到以下异常:

System.InvalidOperationException: This SqlTransaction has completed; it is no longer usable.
at System.Data.SqlClient.SqlTransaction.ZombieCheck()
at System.Data.SqlClient.SqlTransaction.Rollback()
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)

SO上有很多类似的问题,但我读完后无法找出原因。

这是否仅仅是由于 SQL 超时(调用后接近 25 秒发生异常)?或者在这种情况下我应该期待 SQL 超时异常吗?

有谁知道还有什么可能导致这种情况吗?

最佳答案

DataContext.SubmitChanges方法的主体中有以下代码行:

// ...
try
{
if (this.provider.Connection.State == ConnectionState.Open)
{
this.provider.ClearConnection();
}
if (this.provider.Connection.State == ConnectionState.Closed)
{
this.provider.Connection.Open();
flag = true;
}
dbTransaction = this.provider.Connection.BeginTransaction(IsolationLevel.ReadCommitted);
this.provider.Transaction = dbTransaction;
new ChangeProcessor(this.services, this).SubmitChanges(failureMode);
this.AcceptChanges();
this.provider.ClearConnection();
dbTransaction.Commit();
}
catch
{
if (dbTransaction != null)
{
dbTransaction.Rollback();
}
throw;
}
// ...

当连接超时时,catch block 将被执行,并且 dbTransaction.Rollback();该行将抛出 InvalidOperationException .

如果您可以控制代码,则可以捕获如下异常:

catch
{
// Attempt to roll back the transaction.
try
{
if (dbTransaction != null)
{
dbTransaction.Rollback();
}
}
catch (Exception ex2)
{
// This catch block will handle any errors that may have occurred
// on the server that would cause the rollback to fail, such as
// a closed connection.
Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
Console.WriteLine(" Message: {0}", ex2.Message);
}
throw;
}

关于sql-server - ZombieCheck 异常 - 此 SqlTransaction 已完成;它不再可用——在简单提交期间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16692854/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com