How to Apply
Start a transaction
// ------------------------------------------------ Transactionscope -------------------------------------------------------------------
Using (transactionscope scope =
New transactionscope (transactionscopeoption. required, new transactionoptions {isolationlevel = isolationlevel. repeatableread }))
{
// Todo something
// Todo something
// Todo something
Submit the transaction scope. Complete ();
}
The transaction has no rollback. If an exception occurs in the transaction, the transaction will be automatically rolled back, provided that the transaction must be referenced using the using statement.
Transactionscospontion. required transaction type. If a transaction exists in the current environment, it is automatically appended to the current transaction and becomes the subtransaction of the transaction.
The details are as follows:
Required
If a transaction already exists, the transaction scope will be added to the existing transaction. Otherwise, it will create its own transaction.
Requiresnew
This transaction scope will create your own transactions.
Suppress
If the transaction is within the scope of the current active transaction, the transaction scope will neither join the ambient transaction nor create your own transaction. This option can be used when some code needs to be left outside the transaction.
Note:
A connection to multiple different connection strings cannot be enabled in a scope. Otherwise, an exception occurs. solution: to enable a new connection string connection, you can place the sub-connection in a nested scope, as follows:
Using (transactionscope fatherscope =
New transactionscope (transactionscopeoption. required, new transactionoptions {isolationlevel = isolationlevel. repeatableread }))
{
Using (transactionscope chidlscope =
New transactionscope (transactionscopeoption. required, new transactionoptions {isolationlevel = isolationlevel. repeatableread }))
{
// Add a new string connection and perform cross-database operations.
Chidlscope. Complete ();
}
Fatherscope. Complete ();
}
// ------------------------------------------------ Sqltransaction -------------------------------------------------------------------
using (SqlConnection conn = new SqlConnection("conn")) { conn.Open(); using (var tran = conn.BeginTransaction()) { //to do something tran.Commit(); } conn.Close(); }
Compared with sqlconnection. Transaction
1. the scope does not show that the transaction is enabled, so it can only be used once.
2. When the scope transaction reaches a certain level, the transaction will rise to the distributed level.
3. sqltransaction is significantly more efficient at the data layer, and it is convenient to enable and disable transactions when you need to query, improving the overall performance.
4. sqltransaction transactions can only be completed within one connection. scope can be connected across multiple databases (must be the same connection string)
In general
Determine the transaction to be applied based on business needs to ensure data integrity
Tips
Reference: http://www.cnblogs.com/blsong/archive/2010/08/13/1798987.html