Conversion from: EnjoyCode, Enjoy life
Http://www.cnblogs.com/idior/archive/2005/08/15/214300.html
Three types of transactions are introduced before specific implementation:
1. Single Object single resource
2. multi-object single resource
3. multi-object and multi-resource (distributed transaction, using two-segment commit protocol)
There are two ways to use transaction in ADO. net1.0. One is to explicitly call transaction processing in the transaction object,
Another method is to use the declarative method of enterprise service.
The sample code for the first method is as follows:
Public Void Transactiontest ()
{
String Connectionstring = " " ;
Idbconnection connection = New Sqlconnection (connectionstring );
Connection. open ();
Idbcommand command = New Sqlcommand ();
Command. Connection = Connection;
Idbtransaction transaction;
Transaction = Connection. begintransaction (); // Enlisting Database
Command. Transaction = Transaction;
Try
{< br> /**/ /* interact with database here, then commit the transaction
*/
transaction. commit ();
}
Catch
{
Transaction. rollback ();//Abort transaction
}
Finally
{
Connection. Close ();
}
}
This method is quite troublesome to use and can only access one resource for one object. If the transaction involves multiple objects, who will handle the transaction? If the transaction uses multiple resources,
This involves both distributed transactions and two-segment Commit Protocols. In this case, it is too complicated to rely on the first method to be completely controlled by the user. Therefore, after providing the first basic method, ADO. net
1.0 the declarative transaction processing is implemented using COM +. The sample code is as follows:
Using System. incluiseservices;
[Transaction]
Public Class Mycomponent: servicedcomponent
{
[AutoComplete]
Public Void Mymethod ()
{
/**//*Interact with other Serviced components
And resource managers*/
}
}
This declarative method seems good, but it also implies many problems.
1. The transaction object must inherit servicedcomponent
2. Even if a distributed transaction that does not involve multiple resources is only a simple transaction involving multiple objects (the second transaction introduced at the beginning), I also need to use this method, which affects the efficiency.
This kind of disadvantage and the entity in J2EE are both local
Bean communication is similar, and the cool-killing tool has to be used.
3. It is inevitable to use COM +.
4. Use Enterprise
Services transactions are always thread-safe, that is, you cannot allow multiple threads to participate in the same transaction.
The new transaction model provided by ADO. net2.0 combines the advantages of the previous two,
1. You can also use declarative transaction processing methods in simple (non-distributed) transactions,
Instead of using COM + containers, ado.net 2.0 provides a lightweight transaction container.
2. You do not need to consider simple transactions or distributed transactions.
The new model automatically determines the Transaction Manager based on the object resources involved in the transaction. In short, any transaction user can use the same method for processing. Sample Code:
Using (Transactionscope scope Scope = New Transactionscope ())
{
/**//*Perform transactional work here*/
//No errors-commit transaction
Scope. Complete ();
}
In addition, the isolation level of nested transactions and transactions is also supported, so we will not detail it here. Fantasy
Soft introduces this.
Using (Transactionscope scope1 = New Transactionscope ())
// Default is required
{
Using (Transactionscope scope2 = New
Transactionscope (transactionscopeoption. Required ))
{}
Using (Transactionscope scope3 = New
Transactionscope (transactionscospontion. requiresnew ))
{}
Using (Transactionscope scope4 = New
Transactionscope (transactionscospontion. Suppress ))
{}
}
Transactionoptions options = New Transactionoptions ();
Options. isolationlevel = Isolationlevel. readcommitted;
Options. Timeout = Transactionmanager. defaulttimeout;
Using (Transactionscope scope Scope = New Transactionscope (transactionscospontion. required, options ))
Public Enum Isolationlevel
{
Readuncommitted,
Readcommitted,
Repeatableread,
Serializable,
Unspecified,
Chaos,//No isolation whatsoever
Snapshot//Special form of Read committed 8
Supported by SQL Server2005
}
This article is just a brief introduction to ado.net 2.0 transaction. For more information, see the related documentation of M $.
Reference: introducing
System. Transactions by Juval Lowy