代码之家  ›  专栏  ›  技术社区  ›  Akash Kava

它的实际用途是什么系统。交易?

  •  6
  • Akash Kava  · 技术社区  · 15 年前

    我见过 System.Transactions 命名空间,我想知道,我真的可以用这个命名空间的用法来创建RDMBS吗?

    这是MSDN网站上的例子,我知道它可能非常简单,但我无法理解这个示例的好处,有人能告诉我下面这个示例中简单的try/catch和事务范围之间的区别吗。

    如果我要做一个RDBMS(创建我自己的RDMBS),我知道我们必须在磁盘上写很多日志来记录我们执行的操作,最后在回滚的情况下我们会撤消这些操作,但是这里没有任何关于撤消操作的内容。

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

    2 回复  |  直到 15 年前
        1
  •  3
  •   Ladislav Mrnka    15 年前

    首先,TransactionScope与try/catch不同。TransactionScope是事务的名称范围。必须通过在作用域上调用Complete显式提交作用域中的事务。任何其他情况(包括在作用域中引发的异常)都会导致结束使用block,block处理作用域并隐式回滚未完成的事务,但它不会处理异常。

    • API不可知论。您可以对oracle、sql server或web服务使用相同的事务作用域。当事务在不知道持久性的层(不知道任何关于持久性实现的信息)中启动时,这一点很重要。
    • 自动升级到分布式事务。当第二个连接登记到事务时,它将自动升级为分布式连接(需要MSDTC)。当您登记其他协调资源(如事务性web服务)时,升级也起作用。
    • 等。
        2
  •  1
  •   Zaki    15 年前

    事务将为您执行必要的锁定。另外,如果Complete()没有提交事务,则在事务的作用域结束时处理事务时,会有一个隐式回滚(如注释所示)。因此,如果出现异常,所有操作都会自动回滚,数据库中不会发生任何更改。例如,如果第二个查询失败,它还将丢弃第一个查询的更改。

    但是,对于StringWriter,它仍然包含直到出现故障的消息(例如

    Rows to be affected by command1: {0}
    ApplicationException Message: {0}
    

    这两个代码都可以出现在您的日志中。