gpt4 book ai didi

c# - SQLTransaction 已完成错误

转载 作者:可可西里 更新时间:2023-11-01 08:21:25 27 4
gpt4 key购买 nike

我的应用程序中有一次出现以下错误。

This SQLTransaction has completed; it is no longer usable

Stack Trace 附在下面 – 它说明了 Zombie CheckRollback

代码中有什么错误?

注意:此错误只出现一次。

更新

来自 MSDN - SqlTransaction.Rollback Method

A Rollback generates an InvalidOperationException if the connection is terminated or if the transaction has already been rolled back on the server.

来自 Zombie check on Transaction - Error

One of the most frequent reasons I have seen this error showing up in various applications is, sharing SqlConnection across our application.

代码

public int SaveUserLogOnInfo(int empID)
{
int? sessionID = null;
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlTransaction transaction = null;
try
{
transaction = connection.BeginTransaction();
sessionID = GetSessionIDForAssociate(connection, empID, transaction);

//Other Code

//Commit
transaction.Commit();
}
catch
{
//Rollback
if (transaction != null)
{
transaction.Rollback();
transaction.Dispose();
transaction = null;
}

//Throw exception
throw;
}
finally
{
if (transaction != null)
{
transaction.Dispose();
}
}
}

return Convert.ToInt32(sessionID,CultureInfo.InvariantCulture);

}

堆栈跟踪

enter image description here


引用:

  1. What is zombie transaction?
  2. Zombie check on Transaction - Error
  3. SqlTransaction has completed
  4. http://forums.asp.net/t/1579684.aspx/1
  5. "This SqlTransaction has completed; it is no longer usable."... configuration error?
  6. dotnet.sys-con.com - SqlClient Connection Pooling Exposed
  7. Thread abort leaves zombie transactions and broken SqlConnection

最佳答案

你应该把一些工作留给编译器,为你将其包装在 try/catch/finally 中。

此外,如果在 Commit 阶段出现问题,或者与服务器的连接中断,您应该预料到 Rollback 偶尔会抛出异常。因此,您应该将其包装在 try/catch 中。

try
{
transaction.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);
}

这完全是从 MSDN documentation page for Rollback method 复制的.

我看到您担心您有僵尸交易。如果你粘贴了,听起来你没有问题。你的交易已经完成,你不应该再与它有任何关系。如果您持有它们,请删除对它的引用,然后忘记它。


来自 MSDN - SqlTransaction.Rollback Method

A Rollback generates an InvalidOperationException if the connection is terminated or if the transaction has already been rolled back on the server.

重新抛出一个新的异常,告诉用户数据可能没有保存,让她刷新查看

关于c# - SQLTransaction 已完成错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15293673/

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