[英]What is the meaning of Isolation for a transaction that propagates from WCF client to WCF service?
There are three parts to this question: 这个问题分为三个部分:
The code can be downloaded from here 可以从这里下载代码
Assume that the DataLayer (DAL) is implemented by a WCF service and that the client side code consists of a call to its operations from Main: 假定DataLayer(DAL)是由WCF服务实现的,并且客户端代码包括从Main对其操作的调用:
public void Main(string[] args)
{
var dal = new DataLayerServiceClient();
var accounts = dal.GetAccounts();
int accountId = accounts.First().AccountId;
for (int i = 0; i < 10000; i++)
{
using (TransactionScope scope = new TransactionScope())
{
var account = dal.GetAccountById(accountId);
account.Balance++;
dal.Update(account);
scope.Complete();
}
}
}
Assume also: 还假设:
Run two client processes concurrently. 同时运行两个客户端进程。
The expected result is that the balance of the account after both clients exit should be 20000 larger than it was before both clients started. 预期结果是两个客户退出后的帐户余额应比两个客户都启动前的帐户余额大20000。
The balance of the account after both clients exit is a value between 10000 and 20000. In some cases, one of the client is aborted due to the following error: 两个客户端退出后帐户的余额在10000到20000之间。在某些情况下,其中一个客户端由于以下错误而中止:
Transaction (Process ID) was deadlocked on lock resources with another process and has been chosen as the deadlock victim
事务(进程ID)已与另一个进程在锁定资源上发生死锁,并已被选择为死锁受害者
The operations contained within the scope of the TransactionScope on each client did not run as a whole in series with those of the other client. 每个客户端上的TransactionScope范围内包含的操作并未与另一个客户端上的整体串联运行。 Reads and writes from both transactions were mixed and some of the increments were lost.
来自两个事务的读取和写入混合在一起,并且某些增量丢失了。
Question : "If my interpretation of Isolation is not correct, what is the correct interpretation?". 问题 :“如果我对隔离的解释不正确,那么正确的解释是什么?”。
Answer : The incorrect assumption in the question is that if two transactions cannot run concurrently, one will wait for the other to complete and then proceed. 答 :问题中的错误假设是,如果两个事务不能同时运行,则一个事务将等待另一个事务完成然后继续。 This is not correct.
这是不正确的。 Indeed Serializable transactions are isolated and will not run concurrently, but that does not mean that one will wait for the other.
实际上,可序列化事务是隔离的,不会并行运行,但这并不意味着一个将等待另一个。 Isolation of transactions is not the same as holding a mutex in order to perform a set of operations in isolation.
事务隔离与持有互斥锁以独立执行一组操作不同。
Question : "and how could I modify the test to demonstrate the difference between the use of Serialized transaction as oppose to not using a transaction at all" 问题 :“以及如何修改测试以证明使用序列化事务与完全不使用事务之间的区别”
Answer : The following snippet would demonstrate the use of transactions to assure that increment occurs as expected. 答 :以下代码片段将演示如何使用事务来确保按预期进行增量。
int i = 0; while(i < 10000) { try { using (TransactionScope scope = new TransactionScope()) { var account = dal.GetAccountById(accountId); account.Balance++; dal.Update(account); scope.Complete(); } i++; } catch (Exception ex) { Console.WriteLine($"{ex.Message} : restarting"); } }
Of course this is extremely inefficient but it works as expected and demonstrates how transactions isolate operations on resource managers. 当然,这效率极低,但是可以按预期工作,并演示了事务如何隔离资源管理器上的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.