TransactionScope 类

TransactionScope 类

.NET Framework 4
 
 
 
 
1(共 1)对本文的评价是有帮助 - 评价此主题
 

 

使代码块成为事务性代码。 此类不能被继承。

System.Object
  System.Transactions.TransactionScope

 

命名空间:  System.Transactions
程序集:  System.Transactions(在 System.Transactions.dll 中)
复制
public sealed class TransactionScope : IDisposable

TransactionScope 类型公开以下成员。

 名称说明
公共方法 TransactionScope() 初始化 TransactionScope 类的新实例。
公共方法 TransactionScope(Transaction) 初始化 TransactionScope 类的新实例,并将指定的事务设置为环境事务,以便该范围中进行的事务性工作使用此事务。
公共方法 TransactionScope(TransactionScopeOption) 以指定的要求初始化 TransactionScope 类的新实例。
公共方法 TransactionScope(Transaction, TimeSpan) 使用指定的超时时间值初始化 TransactionScope 类的新实例,并将指定的事务设置为环境事务,以便该范围中进行的事务性工作使用此事务。
公共方法 TransactionScope(TransactionScopeOption, TimeSpan) 以指定的超时时间值和要求初始化 TransactionScope 类的新实例。
公共方法 TransactionScope(TransactionScopeOption, TransactionOptions) 以指定的要求初始化 TransactionScope 类的新实例。
公共方法 TransactionScope(Transaction, TimeSpan, EnterpriseServicesInteropOption) 使用指定的超时时间值和 COM+ 互操作性要求初始化 TransactionScope 类的新实例,并将指定的事务设置为环境事务,以便该范围中进行的事务性工作使用此事务。
公共方法 TransactionScope(TransactionScopeOption, TransactionOptions, EnterpriseServicesInteropOption) 使用指定的范围和 COM+ 互操作性要求以及事务选项初始化 TransactionScope 类的新实例。
页首
 名称说明
公共方法 Complete 指示范围中的所有操作都已成功完成。
公共方法 Dispose 结束事务范围。
公共方法 Equals(Object) 确定指定的 Object 是否等于当前的 Object (继承自 Object。)
受保护的方法 Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
公共方法 GetHashCode 用作特定类型的哈希函数。 (继承自 Object。)
公共方法 GetType 获取当前实例的 Type (继承自 Object。)
受保护的方法 MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
公共方法 ToString 返回表示当前对象的字符串。 (继承自 Object。)
页首

System.Transactions 基础结构既提供了基于 Transaction 类的显式编程模型,也提供了使用 TransactionScope 类的隐式编程模型,在后一种模型中,事务由该基础结构自动管理。

重要说明 重要事项

建议使用 TransactionScope 类创建隐式事务,以便自动为您管理环境事务上下文。 对于需要跨多个函数调用或多个线程调用使用相同事务的应用程序,您还应该使用 TransactionScopeDependentTransaction 类。 有关此模型的更多信息,请参见Implementing An Implicit Transaction Using Transaction Scope 主题。 有关编写事务性应用程序的更多信息,请参见Writing A Transactional Application

 

在实例化 TransactionScope 后(通过 new 语句),事务管理器确定哪些事务参与进来。 一旦确定,该范围将始终参与该事务。 此决策基于两个因素:是否存在环境事务以及构造函数中 TransactionScopeOption 参数的值。 您的代码是在环境事务中执行的。 可通过调用 Transaction 类的静态 Current 属性获取对环境事务的引用。 有关如何使用此参数的更多信息,请参见 Implementing An Implicit Transaction Using Transaction Scope 主题的“事务流管理”一节。

如果在事务范围中(即从初始化 TransactionScope 对象到调用其 Dispose 方法之间)未发生异常,则允许该范围所参与的事务继续。 如果事务范围中的确发生了异常,它所参与的事务将回滚。

当应用程序完成它要在一个事务中执行的所有工作以后,您应当只调用 Complete 方法一次,以通知事务管理器可以接受提交事务。 未能调用此方法将中止该事务。

Dispose 方法的调用标志着该事务范围的结束。 在调用此方法之后发生的异常不会影响该事务。

如果在范围中修改 Current 的值,则会在调用 Dispose 时引发异常。 但是,在该范围结束时,先前的值将被还原。 此外,如果在创建事务的事务范围内对 Current 调用 Dispose,则该事务将在相应范围末尾处中止。

下面的示例演示如何使用 TransactionScope 类定义代码块以参与事务。

 

复制
// This function takes arguments for 2 connection strings and commands to create a transaction 
// involving two SQL Servers. It returns a value > 0 if the transaction is committed, 0 if the 
// transaction is rolled back. To test this code, you can connect to two different databases 
// on the same server by altering the connection string, or to another 3rd party RDBMS by 
// altering the code in the connection2 code block.
static public int CreateTransactionScope(
    string connectString1, string connectString2,
    string commandText1, string commandText2)
{
    // Initialize the return value to zero and create a StringWriter to display results.
    int returnValue = 0;
    System.IO.StringWriter writer = new System.IO.StringWriter();

    try
    {
        // Create the TransactionScope to execute the commands, guaranteeing
        // that both commands can commit or roll back as a single unit of work.
        using (TransactionScope scope = new TransactionScope())
        {
            using (SqlConnection connection1 = new SqlConnection(connectString1))
            {
                // Opening the connection automatically enlists it in the 
                // TransactionScope as a lightweight transaction.
                connection1.Open();

                // Create the SqlCommand object and execute the first command.
                SqlCommand command1 = new SqlCommand(commandText1, connection1);
                returnValue = command1.ExecuteNonQuery();
                writer.WriteLine("Rows to be affected by command1: {0}", returnValue);

                // If you get here, this means that command1 succeeded. By nesting
                // the using block for connection2 inside that of connection1, you
                // conserve server and network resources as connection2 is opened
                // only when there is a chance that the transaction can commit.   
                using (SqlConnection connection2 = new SqlConnection(connectString2))
                {
                    // The transaction is escalated to a full distributed
                    // transaction when connection2 is opened.
                    connection2.Open();

                    // Execute the second command in the second database.
                    returnValue = 0;
                    SqlCommand command2 = new SqlCommand(commandText2, connection2);
                    returnValue = command2.ExecuteNonQuery();
                    writer.WriteLine("Rows to be affected by command2: {0}", returnValue);
                }
            }

            // The Complete method commits the transaction. If an exception has been thrown,
            // Complete is not  called and the transaction is rolled back.
            scope.Complete();

        }

    }
    catch (TransactionAbortedException ex)
    {
        writer.WriteLine("TransactionAbortedException Message: {0}", ex.Message);
    }
    catch (ApplicationException ex)
    {
        writer.WriteLine("ApplicationException Message: {0}", ex.Message);
    }

    // Display messages.
    Console.WriteLine(writer.ToString());

    return returnValue;
}


.NET Framework

受以下版本支持:4、3.5、3.0、2.0

.NET Framework Client Profile

受以下版本支持:4、3.5 SP1

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2

 

 

.NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求

此类型是线程安全的。

posted @ 2012-06-09 22:46  Net-Spider  阅读(195)  评论(0)    收藏  举报