In distributed applications, transaction control will inevitably use. There is an end and an end that specifies the border of the transaction, and the transaction can span the processes and computers within its borders. All resources in the transaction border are involved in the same transaction. To maintain consistency between resources within the border of the transaction, the transaction must have an ACID attribute, ie atomic, consistency, isolation, and persistence.
Local affairs and distributed transactions
--------------------
Local Transactions are a transaction (for example, Microsoft SQL Server Database, or MSMQ Message Queue). For example, when a single database system has all data involved in a transaction, you can follow the ACID rules. In the case of SQL Server, the submission and rollback operation of the transaction is implemented by the internal transaction manager.
Distributed transactions can span data resources for different types of identifiable transactions, and can include multiple operations (eg, retrieve data from the SQL database, read messages from Message Queue Server, and write to other databases). Programming for distributed transactions can be simplified by using across several data resources and recoverable software. Microsoft Distributed Transaction Coordinator (DTC) is a technology. It uses a second-stage submission protocol that ensures that transaction results are consistent between all data resources involved in transactions. DTC only supports applications that have implemented compatible interfaces for transaction management. These applications are called resource managers (see the DISTP: //Msdn.microsoft.com/library/en-us/cpguide/html/cpcondistributeTransactions in .NET FRAMEWORK Developer's Guide .asp>;), there are many such applications, including MSMQ, Microsoft SQL Server, Oracle, Sybase, and more.
Database transaction
-------------
If you call a stored procedure that encapsulates the desired operation in the Begin Transaction and Commit / Rollback Transaction statement, you can run your transaction in a single round-trip to the database server to achieve optimal performance. Database transactions also support nested transactions, which means you can launch a new transaction from a active transaction.
In the code snippet below, the Begin Transaction statement starts a new transaction. You can end your transaction by using a commit transaction statement to end your transaction, or if you have any errors, all changes will be revoked by using the Rollback Transaction statement:
CREATE Procedure Proc1
...
AS
- Begin The Transaction
Begin Transaction
- Do Transaction Operations
...
- check for any error
IF @@ Error <> 0
- Rollback The Transaction
Rollback Transaction
...
- Commit the Transaction
Commit transaction
Manual business
-------------
With manual matters, you can explicitly control the transaction boundaries using explicit instructions that start and end transactions. This mode also supports the nested transactions that allow you to start a new transaction from an active transaction. However, this control will add an additional burden to you, you need to register data resources to the transaction border and coordinate these resources. Since there is no built-in support for distributed transactions, if you choose to control distributed transactions in a manual way, you will bear a lot of responsibility; you need to control each connection and resource registration, and the ACID property of the transaction is maintained by providing implementation. . ADO.NET manual business
These two Microsoft ADO.NET data providers start a transaction, submit or abort transaction, and a set of objects that are submitted, submitted, or abort transactions and final closing connections are enabled by providing a connection to the data storage area. We will explain as an example of the ADO.NET SQL hosted provider.
To perform actions in a single transaction, you need to create a SQLTransaction object, starting the transaction using the SqlConnection object, ensuring database interaction between transactions and submits or abort transactions. The SQLTransaction object provides a variety of methods and properties to control transactions. If each of the operations in the transaction has been successfully completed, you can submit the changes to the database using the Submit method. The "rollback" method using the SQLTransaction object rolls back.
Note that the Transaction property of the "Command" object must be set to a transaction that has started so that it can be executed in the transaction.
Visual C # .net
SqlConnection conn = new sqlconnection ("connString");
SQLCommand cmd = new sqlcommand;
// Open a connection
Cn.open ();
// begin a Transaction
Sqltransaction txn = conn.begintransaction ();
// set the transaction in which the commit executes
Cmd.Transaction = TXN;
...
MSMQ manual business
.NET Framework supports MSMQ transactions in two different ways: manually (internal) support by allowing multiple messages as part of transactions; automatic (external) support by participating in the Distributed Transaction Coordinator (DTC) transaction.
The MSMQ manual transaction is supported through the MessageQueTransaction class and is processed within the MSMQ engine. For more information, see Duncan Mackenzie's article Reliable Messaging with msmq and .net
Automatic transaction
-------------
.NET Framework relies on MTS / COM services to support automatic transactions. COM uses Microsoft Distributed Transaction Coordinator (DTC) to run a transaction in a distributed environment as a transaction manager and transaction coordinator. This allows the .NET application to run across multiple resources to combine different operations (for example, inserting a SQL Server database, write messages into Microsoft Message Queuing (MSMQ) queues, send emails, and search data from Oracle databases).
By providing programming models based on declarative transactions, COM makes applications easily run transactions across different kinds of resources. The disadvantage of this approach is that due to the presence of DTC and COM interoperability overhead, the performance can be reduced, and nested transactions are not supported. ASP.NET page, Web Service method, and .NET class can be marked as transactionality by setting declarative transaction attributes.
ASP.NET
<@ Page transaction = "required">
ASP.NET Web Services
<% @ Webservice Language = "VB" Class = "Class1"%>
<% @ askEMBLY Name = "System.EnterpriseServices"%>
...
Public Class Class1
Inherits WebService
Public function method1 ()
...
To participate in automated transactions, the .NET class must be inherited from the System.EnterpriseServices.ServiceDComponent class, which can make the .NET class to run in COM . In this process, the COM is interacting with DTC to create a distributed transaction, and all resource connections in the background. You also need to set up declarative transaction properties to determine its transaction behavior.
Visual C # .net
[Transaction (TransactionOption.Required)]
Public class class1: servicedcomponent {
...
}
The transaction attribute of the class can be set to any of the following:
•
Disabled
- Indicates that the object is not created in a COM transaction. This object can use DTC directly to obtain transactional support.
•
NotSupported
- Indicates that the object is never created in the transaction.
•
"stand by"
- Indicates that the object is run in the context of its creer's transaction. If the object itself is a root object, or its creator is not running in the transaction, the object will be created without the transaction.
•
"required"
- Objects Objects run in the context of its creed's transaction. If the object itself is root object, or its creator is not running in the transaction, the object will use a new transaction to create.
•
Requiresnew
- Indicates that the object requires a transaction, and the object is created with a new transaction.
The following code shows that configured to run in COM , set the assembly property to configure the COM application properties. Net class.
Visual C # .net
Using system;
Using system.Runtime.compilerServices;
Using system.enterprises;
Using system.reflection;
// registration details.
// Com Application Name as it Appears in the COM Catalog
[Assembly: ApplicationName ("class1")]]]
'Strong Name for Assembly
[Assembly: AssemblyKeyFileAttribute ("class1.snk")]
[Assmbly: ApplicationActivation (ActivationOption.server)]
[Transaction (TransactionOption.Required)] public class class1: servicedcomponent {
[AutoComplete]
Public void example1 ()
{
...
}
}
Specifies the name of the COM application to install the assembly of the assembly. Specify whether the COM application is a server application or library application program. When specifying ApplicationActivation (ActiVationOption.server), you must install the assembly to the Global Assembly Cache (GAC) using the GACUTIL command line tool (Gacutil.exe).
You can use the Regsvcs.exe command line tool to convert the assembly to the type library and register the type library and install it into the specified COM application. This tool can also be used to configure the properties you have already added to the program set by programming. For example, if you specify ApplicationActivation (ActivationOption.server) in the program, the tool will create a server application. If you call an assembly without using COM to install an assembly, you will create and register a type library, and use COM to install the library. You can see and configure as a COM application created by the assembly in the Component Service Management unit.
You can get information about the COM object context by using the System.EnterpriseServices.ContextUtil class. It provides SetComplete and Setabort methods to explicitly submit and rollback transactions separately. As you expected, when all operations have been successfully executed, follow the TRY block's final calling the ContextUtil.setComplete method to submit a transaction. Any exception raised will be captured in the Catch block, the block uses the ContextUtil.Setabort to stop the transaction.
You can also use the System.EnterpriseServices.autocomplete property class to allow service components to automatically choose whether to submit transactions or abort transactions. If the method call successfully returns, the component will tend to select submission transactions. If the method calls triggers an exception, the transaction will automatically stop; you don't need to explicitly call ContextUtilSetabort. To use this feature, you should insert the
Visual C # .net
[Transaction (TransactionOption.Required)]
Public class class1: servicedcomponent {
[AutoComplete]
Public void example1 ()
{
...
}
}
In systems that require transactions run across MSMQ and other recognizable transactions (eg, SQL Server databases), you can only use DTC or COM transactions, there is no other option. DTC coordinates all resource manager involved in distributed transactions, also manages operations related to transaction.
summary
------------
Each type of transaction is a trade-off in application performance and code-maintainability. Database transactions running during the stored procedure can provide the best performance because it only needs to go to a single round-trip of the database. In addition, this approach also provides an explicit control of the flexibility of the transaction boundary. Although it provides good performance and flexibility, you need to write code with Transact SQL, which is better to write code with .NET.
Manual transactions using the ADO.NET transaction object is easy to write code and implements the flexibility of starting and ending transactions with explicit instructions to control transaction boundaries. However, in order to achieve this simpleness and flexibility, some additional round trip to the database needs to be completed, which results in performance reduction. If the transaction across multiple managers that identifies transactions (may include SQL Server databases, MSMQ message queues, etc.), automatic transactions will be unique. This method greatly simplifies the application design and reduces the encoding requirements. However, because Com services perform all coordination work, there may be some additional overhead.
More resources:
------------
Writing serviced Components
Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpConwritingServicedComponents.asp
Processing Transactions
Http://msdn.microsoft.com/library/en-us/cpguide/html/cpconprocessingtransactions.asp