In. NET Framework 2.0, a namespace is added: system. Transactions. It can be seen from its name that it contains transaction-related classes. System. Transactions provides a "lightweight" and easy-to-use transaction framework.
In the past, to implement transaction, you had to use javasiseservices to let components inherit from servicecomponent. With system. Transactions, as long as a few lines of code are simple, there is no need to inherit, and no attribute mark is required.
The following describes the simplest (or most common) usage of system. transactions:
Using (transactionscope Ts = new transactionscope ())
{
// Compile the code that requires transaction.
TS. Consistent = true;
}
The transactionscope class is used to construct a transaction scope. The code in this scope will be capable of transaction. Transactionscope implements idisposable. When transactionscope. Dispose () is called, if the consistent attribute is not set to true, the rollback action is triggered.
Using (transactionscope Ts = new transactionscope ())
{
Using (sqlconnection conn = new sqlconnection ("..."))
{
Conn. open ();
}
TS. Consistent = true;
}
The code above demonstrates opening a database connection in a transaction scope. This database connection is in a transaction scope, so it will automatically obtain the transaction capability. If the database is connected to sqlserver2005, this transaction will not activate a Distributed Transaction managed by MSDTC, but will create a local transaction by. net, with high performance. However, if it is sqlserver2000 or 7, a distributed transaction will be automatically activated, which will suffer some performance loss.
Using (transactionscope Ts = new transactionscope ())
{
Using (sqlconnection conn = new sqlconnection ("..."))
{
Conn. open ();
Using (sqlconnection conn2 = new sqlconnection ("..."))
{
Conn2.open ();
}
}
TS. Consistent = true;
}
This example fully demonstrates the powerful transaction scope and the connection between two databases! Although the preceding conn and conn2 are two different connection objects, they may be connected to different databases, but because they are in a transaction scope, they have the "linkage" transaction capability. Here, a distributed transaction managed by MSDTC is automatically activated. (You can open the component service in the management center to view the current distributed transaction list .)
Next we will introduce how to manually add a resource to a distributed transaction:
Icommittabletransaction TR = Transaction. Create ();
Using (sqlconnection conn = new sqlconnection ("..."))
{
Conn. enlisttransaction (TR as itransaction );
}
Tr. Commit ();
The above Code creates an icommittabletransaction object manually (using the static method of the transaction class ). The sqlconnection object participates in the transaction through the enlisttransaction () method. Note: The enlisttransaction () method only accepts the itransaction type. Because itransaction does not have the Commit () method, you certainly do not want other objects other than icommittabletransaction to execute the Commit () method.
Reference resource link:
System. Transactions namespace
Msdntv: introducing system. Transactions in. NET Framework 2.0