如何处置的SqlTransaction前检查状态 [英] How to check state before disposing SqlTransaction

查看:192
本文介绍了如何处置的SqlTransaction前检查状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下使用的SqlTransaction code。我呼吁处置的渔获物和finally块。但我没有检查它是否已经部署调用Dispose()之前。我们如何检查的SqlTransaction是否已经部署调用Dispose()之前?

我已经提到 MSDN:SqlTransaction.Dispose方法。但是,这不包括我的问题。

也称为<一个href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction.dispose(v=vs.100).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction.dispose(v=vs.100).aspx

注:我已经知道了的TransactionScope 的SqlTransaction 具有优势。但我想了解的SqlTransaction的处置。

code

 使用(SqlConnection的连接=新的SqlConnection(的connectionString))
        {
            connection.Open();

            的SqlTransaction事务= NULL;

            尝试
            {
                交易= connection.BeginTransaction();

                会话ID = GetSessionIDForAssociate(连接,EMPID,交易);
                //其他code

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

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

解决方案
  我们

如何检查的SqlTransaction是否已经部署之前,   调用Dispose()?

您不必。两次调用处理不会给你任何问题。

处置 - MSDN

  

如果一个对象的处置方法被调用一次以上时,对象   必须忽略在第一个之后的所有呼叫。该对象不能丢   一个异常,如果它的Dispose方法被调用多次。实例   方法比其他处置可以抛出的ObjectDisposedException时   资源已经处置。

但是,如果你想只调用Dispose一次,那么你可以使用一个布尔标志当交易被设置来设置,也可以将其设置为null。或删除呼叫处理的,因为 块最后块将始终被调用。

由于的SqlTransaction工具 IDisposable的其更好如果你使用它使用块。是这样的:

 使用(SqlConnection的连接=新的SqlConnection(的connectionString))
{
    connection.Open();

    使用(的SqlTransaction事务= connection.BeginTransaction())
    {
        尝试
        {
            会话ID = GetSessionIDForAssociate(连接,EMPID,交易);
            //其他code

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

            //抛出异常
            扔;
        }
    }
}
 

由于使用块的行为像尝试/终于块,并将确保其完成时出售交易(即使发生异常)。所以,您不必手动调用处置

I have following code that uses SqlTransaction. I have called dispose in catch and finally blocks.. But I have not checked whether it is already disposed before calling Dispose(). How can we check whether the SqlTransaction is already disposed before calling Dispose()?

I have referred MSDN:SqlTransaction.Dispose Method. But that does not cover my question.

Also referred http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction.dispose(v=vs.100).aspx

Note: I already know that TransactionScope has advantages over SqlTransaction. But I am trying to understand the SqlTransaction’s dispose.

CODE

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

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

解决方案

How can we check whether the SqlTransaction is already disposed before calling Dispose()?

You don't have to. Calling dispose twice won't cause you any problem.

Dispose - MSDN

If an object's Dispose method is called more than once, the object must ignore all calls after the first one. The object must not throw an exception if its Dispose method is called multiple times. Instance methods other than Dispose can throw an ObjectDisposedException when resources are already disposed.

But if you want to only call Dispose once then you can either use a boolean flag to set when the transaction is disposed or you can set it to null. Or remove the call to dispose in catch block since finally block will always be called.

Since SqlTransaction implements IDisposable, its better if you use it with using block. Something like:

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();

    using (SqlTransaction transaction = connection.BeginTransaction())
    {
        try
        {
            sessionID = GetSessionIDForAssociate(connection, empID, transaction);
            //Other code

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

            //Throw exception
            throw;
        }
    }
}

Since using block acts like try/finally block, and it will ensure the disposal of the transaction upon its completion (even if the exception occurs). So you don't have to manually call Dispose.

这篇关于如何处置的SqlTransaction前检查状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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