c# - 如何在处理 SqlTransaction 之前检查状态

标签 c# .net ado.net

我有以下使用 SqlTransaction 的代码。我已经在 catch 和 finally block 中调用了 dispose。但是我没有在调用 Dispose() 之前检查它是否已经被处理。我们如何在调用 Dispose() 之前检查 SqlTransaction 是否已经被释放?

我提到了MSDN:SqlTransaction.Dispose Method .但这不包括我的问题。

还提到了 http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction.dispose(v=vs.100).aspx

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

代码

 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()?

你不必。调用 dispose 两次不会给你带来任何问题。

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.

但是如果您只想调用 Dispose 一次,那么您可以使用 bool 标志来设置事务处理的时间,也可以将其设置为 null。或者在 catch block 中删除对 dispose 的调用,因为 finally block 将始终被调用。

由于 SqlTransaction 实现了 IDisposable , 如果将它与 using block 一起使用会更好。像这样的东西:

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

由于using block 的行为类似于try/finally block,它会确保在交易完成时对其进行处理(即使发生异常)。因此您不必手动调用 Dispose

关于c# - 如何在处理 SqlTransaction 之前检查状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14438736/

相关文章:

c# - 为什么 GridView 不在 asp :Panel 中显示数据

c# - .NET 中类型的定义

c# - 通用字典检索值

c# - 如何将用户提供的输入添加到 SQL 语句?

java - EJB 是 Java 中的 ADO.NET 等价物吗?

c# - 向我解释这个 SELECT dbo.TableName(@variable) 语句

c# - 如何在 C# 中获取应用程序的安装日期

c# - 我如何分析 C# prodump 崩溃转储文件,异常来自 CLR?

.net - F# 函数返回值

.net - 普通的exe文件和.net windows应用程序生成的exe文件有什么区别