日期:2009-02-12 浏览次数:20546 次
MSMQ manual transactions are supported through the MessageQueueTransaction class and are handled entirely inside the MSMQ engine. Please refer to Duncan Mackenzie's article, Reliable Messaging with MSMQ and .NET, for details.
Automatic Transactions
The .NET Framework relies on MTS/COM+ services to support automatic transactions. COM+ uses the Microsoft Distributed Transaction Coordinator (DTC) as a transaction manager and a transaction coordinator to run transactions in a distributed environment. This enables a .NET application to run a transaction that combines diverse activities across multiple resources such as inserting an order into a SQL Server database, writing a message to a Microsoft Message Queue (MSMQ) queue, sending an e-mail message, and retrieving data from an Oracle database.
By providing a programming model based on declarative transactions, COM+ makes it very simple for your application to run transactions that span heterogeneous resources. The caveat is that it pays a performance penalty due to DTC and COM interoperability overhead and there is no support for nested transactions.
ASP.NET pages, Web Service methods, and .NET classes can be marked to be transactional by setting a declarative transaction attribute.
ASP.NET
<@ Page Transaction="Required">
ASP.NET Web Service
<%@ WebService Language="VB" Class="Class1" %>
<%@ assembly name="System.EnterpriseServices" %>
…
Public Class Class1
Inherits WebService
<WebMethod(TransactionOption := TransactionOption.RequiresNew)> _
Public Function Method1()
…
To participate in automatic transactions, a .NET class must be inherited from the System.EnterpriseServices.ServicedComponent class, which enables it to run inside COM+. When you do this, COM+ interacts with the DTC to create a distributed transaction and also enlists all resource connections behind the scenes. You also need to set a declarative transaction attribute on the class to determine its transactional behavior.
Visual Basic .NET
<Transaction(TransactionOption.Required)> Public Class Class1
Inherits ServicedComponent
Visual C# .NET
[Transaction(TransactionOption.Required)]
public class Class1 : ServicedComponent {
…
}
The transaction attribute for a class can be set to any of the following options:
Disabled—indicates that the object is never created in a COM+ transaction. The object can directly use DTC for transactional support.
NotSupported—indicates that the object is never created in a transaction.
Supported—indicates that the object runs in the context of its creator's transaction. If the object is the root object itself or its creator is not running in a transaction, the object is created without a transaction.
Required—indicates that the object runs in the context of its creator's transaction. If the object is the root object itself or its creator is not running in a transaction, the object is created with a new transaction.
RequiresNew—indicates that the object requires a transaction and the object is created with a new transaction.
The following code shows a .NET class configured to run within COM+, with assembly attributes set to configure the COM+ application properties.
Visual Basic .NET
Imports System
Imports System.Runtime.CompilerServices
Imports System.EnterpriseServices
I