TransactionScope can be used in a separate scene
The System.Transactions infrastructure provides both an explicit programming model based on the Transaction class and an implicit programming model that uses the TransactionScope class, in which the transaction is automatically managed by the underlying structure.
Important NOTE: |
It is recommended that you create an implicit transaction using the TransactionScope class to automatically manage the environment transaction context for you. You should also use the TransactionScope and DependentTransaction classes for applications that use the same transaction to call across multiple function calls or multiple threads. For more information about this model, see Implementing an Implicit Transaction topic with transaction scope. For more information about writing a transactional application, see Writing a transaction application. |
When TransactionScope is instantiated with the new statement, the transaction manager determines which transaction to participate in. Once determined, this scope will always participate in the transaction. This decision is based on two factors: whether the environment transaction exists and the value of the TransactionScopeOption parameter in the constructor. An environment transaction is a transaction in which your code is executed. You can obtain a reference to an environment transaction by calling the current static property of the Transaction class. For more information about how to use this parameter, see the "Transaction Flow Management" section of implementing implicit transaction topics using transaction scopes.
If no exception occurs in the transaction scope (that is, between the initialization of the TransactionScope object and the call to its Dispose method), the transaction to which the scope participates is allowed to continue. If an exception does occur in the transaction scope, the transaction to which it participates is rolled back.
After the application completes all the work it wants to do in a transaction, you should only call the Complete method once to inform the transaction manager that it can accept the commit transaction. Failure to call this method will abort the transaction.
A call to the Dispose method marks the end of the scope of the transaction. The exception that occurs after this method is called does not affect the transaction.
If you modify the value of current in a scope, an exception is thrown when you call Dispose. However, at the end of the range, the previous value will be restored. In addition, if Dispose is called to current within the transaction scope of the transaction being created, the transaction is aborted at the end of the corresponding range.
First, the advantages of TransactionScope
1, the use of more convenient. TransactionScope can implement implicit transactions so that you can write data access layer code without taking into account the transaction, while at the business level of the control of the transaction.
2, can implement distributed transactions, such as cross-Library or MSMQ.
Second, TransactionScope shortcomings
1, the price is not high. For example, you just control a library transaction in "Scope." It's a bit wasteful to use "TransactionScope".
2, in general, as long as you use "TransactionScope", you have to configure MSDTC, to be equipped with a firewall, to open 139 ports. This port cannot be changed
If you have to use distributed services, then you have to ponder
1. This operation must be in the business. If this operation is not completed or fails, it is worth rolling back the entire transaction. There is no elegant compensation or fault-tolerant measures.
2. Distributed transactions involve points that must be so much. Must have to operate this large string in real time. It is not possible to streamline some points by notifying class operations.
3. After initiating a distributed transaction, you are not doing transaction-independent operations, although these operations are not related to transactions. (for example, read data, calculate, wait for a user to return a message, wait for other modules to return the call, and so on) to know that the transaction should end as soon as possible.
4. You have not put some reading operations in the business. This is a very easy mistake to make, and you enlist a select operation in a transaction.
5. Your operation, some steps can be completed after the completion of all operations. This kind of operation has obvious notice class characteristic. Notification class operation is to say, I give you a notice, and I promise to inform you, you must eat this notice, and ensure the success of the processing, but you do not have to notify you as soon as you deal with. Such an operation would obviously be done with a different task.
Iv. using Distributed transactions note the following points
1: Ensure that the machine of participating transactions open the Distributed transaction support;
2: If the machine firewall, you need to set the MSDTC process as an exception;
3: The machine of participating in the transaction cannot cross domain (if cross-domain, currently Microsoft does not have the exact solution);
4: Multiple databases to use distributed transactions, if it is the same database, it is best to use sqltransaction.
V. TransactionScope transaction level
The default transaction level in TransactionScope is serializable, that is, the complete lock table in the transaction process. Other processes cannot be queried, modified, added, deleted. This can lead to a significant reduction in efficiency, although data integrity is high. Usually we don't need that high data integrity. So the default transaction level needs to be modified:
All transaction levels are as follows
Chaos
Cannot overwrite pending changes in higher isolation level transactions.
ReadCommitted
Variable data cannot be read during a transaction, but it can be modified.
ReadUncommitted
Variable data can be read and modified during a transaction.
RepeatableRead
Variable data can be read during a transaction, but cannot be modified. You can add new data during a transaction.
Serializable
Variable data can be read during a transaction, but cannot be modified or any new data can be added.
Snapshot
Variable data can be read. Before a transaction modifies data, it verifies whether another transaction has changed the data after it was originally read. If the data has been updated, an error is raised. This enables the transaction to obtain the previously committed data value.
When an attempt is made to promote a transaction created with this isolation level, a InvalidOperationException is thrown and an error message "Transactions with IsolationLevel Snapshot cannot is Promoted (cannot promote a transaction with a IsolationLevel snapshot).
Unspecified
The isolation level that is different from the specified isolation level is being used, but the level cannot be determined. If this value is set, an exception is thrown.
Vi. using the TransactionScope class (distributed transactions) in C #, you should note the following
1. Reference the using System.Transactions namespace in the project (first to the reference to the Add net component);
2, the MSDTC component settings:
In the Control Panel-> management tool-> Service, open distributed Transaction Coordinator service.
A, Control Panel-> management Tools-> Component Services-> computer-> My Computer-> right key-> properties
b, select the MSDTC page and confirm the "use local coordinator"
C, click the "Security Configuration" button below
D, tick: "Allow network DTC Access", "Allow remote client", "Allow inbound", "Allow Outbound", "Do not require authentication."
E, for the database server side, you can select "Require caller Authentication"
F, check: Enable transaction Internet Protocol (TIP) transactions.
G, add MSDTC.exe exception in both firewalls
Available command line: netsh firewall set allowedprogram%windir%/system32/msdtc.exe MSDTC enable
3, restart the IIS server.
Attention:
We just have to make sure that the open operation of the database is open within the scope of the transaction. This will enable the correct operation of the transaction.
If the Web server and database are on the same server, TransactionScope is using a local transaction, there is no need to configure MSDTC.
If the Web server and database are not on the same server, TransactionScope automatically promotes the transaction level as a distributed transaction, and then you need to configure MSDTC.
Vii. Examples of TransactionScope
C # code replication
/**////<summary>///Send message///</summary>///<param name= "Senduserid" ></param> <param name= "Touser" > Format 7ffa3af2-e74b-4174-8403-5010c53e49a7|username, 7ffa3af2-e74b-4174-8403-5010c53e49a7|username</param>///<param name= "content" ></param>/// ;p Aram Name= "Sendedstatus" > indicates sent </param>///<returns></returns> public static int SendMessage
(String Senduserid, String touser, string content, String sendedstatus)
{int receivecount = 0;
Transactionoptions transactionoption = new Transactionoptions ();
Set TRANSACTION ISOLATION Level transactionoption.isolationlevel = System.Transactions.IsolationLevel.ReadCommitted;
Set the transaction timeout to 60 seconds transactionoption.timeout = new TimeSpan (0, 0, 60);
using (TransactionScope scope = new TransactionScope (transactionscopeoption.required, transactionoption)) {
try { Here to implement transactional work//Send Message Insertmessage (Senduserid, Touser, Content, sendedstatus);
Insert a record in the Receive information table Receivecount + = Insertreceivemessage (Userids[0], Senduserid, content, "0");
No error, COMMIT transaction scope.complete (); The catch (Exception ex) {throw new Exception ("Send an information exception, cause:" +ex.
message); }finally{//Release resource scope.
Dispose ();
} return Receivecount; }
Similar instance:
Modify order status protected void Updatestate_click (object sender, EventArgs e) {String id = Request .
querystring["id"];
int x; if (int. TryParse (ID, out x)) {DAL. Orderdao Orderdao = new DAL.
Orderdao (); Model.order od = Orderdao.
Getmodel (x);
if (od!=null) {switch (od.state) { Case 0:if (Chkstate.items[1]. Selected = = True) {Orderdao.
Updatestate (od.id, 1); Utility.Tool.alert ("Status update Succeeded", Request.Url.ToString (), this.
Page); else {Utility.tool. Alert ("Please tick the user has designated the paragraph", this.)
Page); Return
} break; Case 3:if (Chkstate.items[4].
Selected = = true) {//here to complete the order, to add points to the user, we use transactions to deal with
using (TransactionScope scope=new TransactionScope ())
{try { Orderdao.
Updatestate (Od.id, 4);
Add the amount of the item in the order to the user's points.
int jifen = convert.toint16 (Od.detailsmoney); New DAL. Userdao ().
Updateintegral (User.Identity.Name, Jifen); Utility.Tool.alert ("Status update successful, integral" + Jifen + "cent has been sent",
Request.Url.ToString (), this.
Page); After processing the transaction, remember to submit, otherwise, he still does not execute the scope.
Complete ();
catch (Exception re) { Response.Write ("Transaction error, please contact admin" +re.)
message);
Response.End ();
}
}
} else {Utility.Tool.alert ("Please tick the user has received the goods", this.)
Page);
Return
} break; Default:break; }
}
}
}
Compared to other types of transactions
1. SQL transaction
Benefits: Best Execution efficiency
Restrictions: Transaction contexts are invoked only in the database, and complex business logic is difficult to implement.
[SQL]
View Plain
Copy