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



下面是堆栈跟踪的内容–它说明了Zombie CheckRollback

代码中的错误是什么?

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

更新

MSDN - SqlTransaction.Rollback Method



Zombie check on Transaction - Error



CODE

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);

   }

堆栈跟踪



引用:
  • What is zombie transaction?
  • Zombie check on Transaction - Error
  • SqlTransaction has completed
  • http://forums.asp.net/t/1579684.aspx/1
  • "This SqlTransaction has completed; it is no longer usable."... configuration error?
  • dotnet.sys-con.com - SqlClient Connection Pooling Exposed
  • Thread abort leaves zombie transactions and broken SqlConnection
  • 最佳答案

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

    此外,如果在Rollback阶段出现问题,或者与服务器的连接中断,您应该期望Commit偶尔会引发异常。因此,您应该将其包装在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



    重新引发新的异常以告知用户可能尚未保存数据,并要求她刷新和查看

    10-08 10:56