的SqlTransaction已完成错误 [英] SQLTransaction has completed error

查看:1596
本文介绍了的SqlTransaction已完成错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序一次以下错误。

  

本的SqlTransaction已完成;它不再可用

堆栈跟踪附在下面 - 它说,约僵尸检查还原

什么是在code中的错误?

注:此错误只来一次,

更新

MSDN - SqlTransaction.Rollback方法

  

回滚生成一个InvalidOperationException如果连接被终止,或者如果事务已回滚在服务器上。

从<一个href="http://blogs.msdn.com/b/dataaccesstechnologies/archive/2010/08/24/zombie-check-on-transaction-error-this-sqltransaction-has-completed-it-is-no-longer-usable.aspx"相对=nofollow>僵尸检查的事务 - 错误

  

其中,最常见的原因,我已经看到了这个错误出在各种应用起来就是,共享的SqlConnection在我们的应用程序。

code

 公众诠释SaveUserLogOnInfo(INT EMPID)
{
        诠释?会话ID = NULL;
        使用(SqlConnection的连接=新的SqlConnection(的connectionString))
        {
            connection.Open();
            的SqlTransaction事务= NULL;
            尝试
            {
                交易= connection.BeginTransaction();
                会话ID = GetSessionIDForAssociate(连接,EMPID,交易);

                    //其他code

                //承诺
                器transaction.commit();
            }
            抓住
            {
                //回滚
                如果(交易!= NULL)
                {
                    transaction.Rollback();
                    transaction.Dispose();
                    交易= NULL;
                }

                //抛出异常
                扔;
            }
            最后
            {
                如果(交易!= NULL)
                {
                    transaction.Dispose();
                }
            }
        }

        返回Convert.ToInt32(会话ID,CultureInfo.InvariantCulture);

   }
 

堆栈跟踪


参考

  1. 什么是僵尸交易?
  2. <一个href="http://blogs.msdn.com/b/dataaccesstechnologies/archive/2010/08/24/zombie-check-on-transaction-error-this-sqltransaction-has-completed-it-is-no-longer-usable.aspx"相对=nofollow>上交易僵尸检查 - 错误
  3. 的SqlTransaction已完成
  4. http://forums.asp.net/t/1579684.aspx/1
  5. <一个href="http://stackoverflow.com/questions/6358806/this-sqltransaction-has-completed-it-is-no-longer-usable-configuration-er">"This的SqlTransaction已完成;它不再可用&QUOT;?...配置错误
  6. dotnet.sys-con.com - SqlClient连接池暴露
  7. <一个href="http://stackoverflow.com/questions/6219298/thread-abort-leaves-zombie-transactions-and-broken-sqlconnection">Thread中止叶僵尸交易和破碎的SqlConnection

解决方案

您应该留下一些工作,编译器,包裹在一个尝试 / / 最后给你。

此外,你应该预料到还原偶尔会抛出一个异常,如果出现问题提交的阶段,或者,如果服务器断开连接。出于这个原因,你应该把它包在尝试 /

 尝试
{
    transaction.Rollback();
}
赶上(例外EX2)
{
    //这个catch块来处理可能发生的任何错误
    //服务器,将导致回滚失败,例如上
    //关闭的连接。
    Console.WriteLine(回滚异常类型:{0},ex2.GetType());
    Console.WriteLine(消息:{0},ex2.Message);
}
 

这是完全从回滚方法 MSDN文档页面复制。

我看你是担心你有一个僵尸交易。如果你粘贴的,它听起来并不像你有问题。你是交易已经完成了,你不应该再有什​​么关系呢。删除引用它,如果你抱着他们,忘掉它。


MSDN - SqlTransaction.Rollback方法

  

回滚生成一个InvalidOperationException如果连接被终止,或者如果事务已回滚在服务器上。

重新抛出一个新的异常,告诉用户该数据可能还没有被保存,并要求她刷新和审查

I got following error once in my application.

This SQLTransaction has completed; it is no longer usable

Stack Trace is attached below – It says about Zombie Check and Rollback.

What is the mistake in the code?

Note: This error came only once.

UPDATE

From 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.

From 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.

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

   }

Stack Trace


REFERENCE:

  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


解决方案

You should leave some of the work to compiler, to wrap that in a try/catch/finally for you.

Also, you should expect that Rollback can occasionally throw an exception, if a problem occurs in Commit stage, or if a connection to server breaks. For that reason you should wrap it in a 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);
}

This is copied exactly from MSDN documentation page for Rollback method.

I see that you're worried that you have a zombie transaction. In case you pasted, it doesn't sound like you have a problem. You're transaction has been completed, and you should no longer have anything to do with it. Remove references to it if you hold them, and forget about it.


From 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.

Rethrow a new exception to tell user that data may not have been saved, and ask her to refresh and review

这篇关于的SqlTransaction已完成错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆