Chapter WCF service behavior conduct affairs affairs

Defined in the behavior of the operating layer TransactionScopeRequired and TransactionAutoComplete this chapter is described in the previous section. In the service layer, there are two additional acts need to be considered: TransactionIsolationLevel and TransactionTimeout.

  TransactionIsolationLevel property, as the name implies, affect isolation level of the transaction. Isolation refers to the I in ACID transactions and guidance on how its surroundings isolation. There are a lot of isolation level. Serializable, by default, to provide the highest level of isolation and prevent other parts of the updated data until the transaction is complete. For example, if the transaction contains a "select count (*) from orders" statements, no other process on the orders table can insert or delete any data until the transaction is complete. ReadUncommitted offers the lowest level of isolation, allow any other process of being updated in a transaction before the transaction completes the data read or write. In practical applications, it is preferable to retain the isolation level to the default value, Serializable, corresponding to avoid transaction locks non-essential data, like select (*) from order this situation should be avoided.

  The transaction isolation level must be consistent across TransactionScope defined by the client and TransactinIsolationLevel defined by the service behavior. If you do not have a set value is, by default, IsolationLevel.Serializable will be used. Listing 5.22 shows the settings of the client and server, each of which is ReadUncommitted.

  A transaction may run out of time can also be controlled. This can be a client or server settings, they have different purposes. In the client, which it can be used to limit the sum of the operation of a user-initiated transaction runtime. In the server, which can be determined by the system administrator to set more transactions not have to use too many resources.

  In Listing 5.22, the client TransactionScopeOption.Timeout is set to 30 seconds, meaning that the user-defined transaction should be launched after it runs for more than 30 seconds. Also lists 5.22, the server ServiceBehavior, TransactionTimeout is set to 1 minute, meaning that if there are any transactions to run more than one minute, it will automatically terminate.

Listing 5.22 and set the transaction isolation level timeout

01 //
02 //Client code
03 //
04 TransactionOptions opt = new TransactionOptions();
05 opt.IsolationLevel = IsolationLevel.ReadUncommitted;
06 opt.Timeout = new TimeSpan(0, 0, 30);
07 using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew, opt))
08 {
09     localhost1.BankServiceClient proxy = new Client.localhost1.BankServiceClient();
10     proxy.Transfer("savings", "checking", 100);
11     scope.Complete();
12     proxy.Close();
13 }
1 //
2    //Service code
3    //
4    [ServiceBehavior(TransactionIsolationLevel = IsolationLevel.ReadUncommitted, TransactionTimeout="00:01:00")]
5    public class BankService : IBankService


=====

forward from

 

Reproduced in: https: //www.cnblogs.com/llbofchina/archive/2011/06/30/2094087.html

Guess you like

Origin blog.csdn.net/weixin_34006965/article/details/94206819