public static void TestAddDeleteLockData(int NumUser, int n, ITestService s) { //如果在不使用CascadeLock和TransactionLock的情况下 //(将Program.cs中的 //var dam = new DataAccessManager(ConnectionString, cl); //替换为 //var dam = new DataAccessManager(ConnectionString, null); //) //部分数据库会出现死锁问题 //测试锁定不存在的行 //目前测试的数据库 //SQL Server 2012 通过 //MySQL 5.5.29 较难出现死锁 //MySQL 5.6.14 死锁 //MySQL 5.7.2 死锁 //PostgreSQL 9.1 异常 //必须使用锁或者重试 if (n % 2 == 0) { s.AddLockData(1); } else { var v = s.DeleteLockData(); lock (Lockee) { SumValue += v + 1; } } }
public void AddLockData(int Value) { var RetryCount = 0; while (RetryCount < MaxRetryCount) { try { Inner.AddLockData(Value); return; } catch (Exception ex) { if (IsRetryable(ex)) { RetryCount += 1; continue; } else { System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(ex).Throw(); throw; } } } throw new InvalidOperationException("MaxRetryCountReached"); }
public static void TestAddLockData(int NumUser, int n, ITestService s) { s.AddLockData(1); }