0

我们有一个企业应用程序,我们在其中调用数据库 1,调用 Web 服务,然后调用数据库 2,所有这些都在一个事件序列中。我们想将整个处理过程封装在一个事务中。在这种情况下实现分布式事务的最佳方法是什么?

环境:SQL 2008,ASP.Net 3.5

4

1 回答 1

2

使用一个TransactionScope对象和不同的连接(每个数据库一个)。事务将自动升级为分布式事务。

从 MSDN 页面上的示例:

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

            using (SqlConnection connection2 = new SqlConnection(connectString2))
            {
                // The transaction is escalated to a full distributed
                // transaction when connection2 is opened.
                connection2.Open();
            }
        }

        scope.Complete();
    }
于 2011-02-25T08:47:50.383 回答