扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
1using (TransactionScope ts= new TransactionScope()) 2{ 3 //自定义操作 4 ts.Complete(); 5} |
1class SampleEnlistment1 : IEnlistmentNotification 2{ 3 void IEnlistmentNotification.Commit(Enlistment enlistment) 4 { 5 Console.WriteLine("提交!"); 6 enlistment.Done(); 7 } 8 9 void IEnlistmentNotification.InDoubt(Enlistment enlistment) 10 { 11 throw new Exception("The method or operation is not implemented."); 12 } 13 14 void IEnlistmentNotification.Prepare(PreparingEnlistment preparingEnlistment) 15 { 16 Console.WriteLine("准备!"); 17 preparingEnlistment.Prepared(); 18 } 19 20 void IEnlistmentNotification.Rollback(Enlistment enlistment) 21 { 22 Console.WriteLine("回滚!"); 23 enlistment.Done(); 24 } 25} 26 27 |
1using (TransactionScope ts= new TransactionScope()) 2{ 3 SampleEnlistment1 myEnlistment1 = new SampleEnlistment1(); 4 Transaction.Current.EnlistVolatile(myEnlistment1, EnlistmentOptions.None); 5 ts.Complete(); 6} |
1class SampleEnlistment2 : IEnlistmentNotification 2{ 3 public SampleEnlistment2(AssignTransactionDemo var, int newValue) 4 { 5 _var = var; 6 _oldValue = var.i; 7 _newValue = newValue; 8 } 9 10 private AssignTransactionDemo _var; 11 private int _oldValue; 12 private int _newValue; 13 14 void IEnlistmentNotification.Commit(Enlistment enlistment) 15 { 16 _var.i = _newValue; 17 Console.WriteLine("提交!i的值变为:" + _var.i.ToString()); 18 enlistment.Done(); 19 } 20 21 void IEnlistmentNotification.InDoubt(Enlistment enlistment) 22 { 23 throw new Exception("The method or operation is not implemented."); 24 } 25 26 void IEnlistmentNotification.Prepare(PreparingEnlistment preparingEnlistment) 27 { 28 preparingEnlistment.Prepared(); 29 } 30 31 void IEnlistmentNotification.Rollback(Enlistment enlistment) 32 { 33 _var.i = _oldValue; 34 Console.WriteLine("回滚!i的值变为:" + _var.i.ToString()); 35 enlistment.Done(); 36 } 37} 38 39class AssignTransactionDemo 40{ 41 public int i; 42 43 public void AssignIntVarValue(int newValue) 44 { 45 SampleEnlistment2 myEnlistment2 = new SampleEnlistment2(this, newValue); 46 Guid guid = new Guid("{3456789A-7654-2345-ABCD-098765434567}"); 47 Transaction.Current.EnlistDurable(guid, myEnlistment2, EnlistmentOptions.None); 48 } 49} 50 51 |
1AssignTransactionDemo atd = new AssignTransactionDemo(); 2atd.i = 0; 3using (TransactionScope scope1 = new TransactionScope()) 4{ 5 atd.AssignIntVarValue(1); 6 Console.WriteLine("事务完成!"); 7 scope1.Complete(); 8 Console.WriteLine("退出区域之前,i的值为:" + atd.i.ToString()); 9} 10Thread.Sleep(1000); 11Console.WriteLine("退出区域之后,i的值为:" + atd.i.ToString()); |
1class SampleEnlistment2 : IEnlistmentNotification 2{ 3 void IEnlistmentNotification.Commit(Enlistment enlistment) 4 { 5 enlistment.Done(); 6 } 7 //其它略 8} 9 10class AssignTransactionDemo 11{ 12 public int i; 13 14 public void AssignIntVarValue(int newValue) 15 { 16 SampleEnlistment2 myEnlistment2 = new SampleEnlistment2(this, newValue); 17 Guid guid = new Guid("{3456789A-7654-2345-ABCD-098765434567}"); 18 Transaction.Current.EnlistDurable(guid, myEnlistment2, EnlistmentOptions.None); 19 i = newValue; 20 Console.WriteLine("提交前改变!i的值为:" + i.ToString()); 21 } 22} 23 24 |
1using (TransactionScope scope1 = new TransactionScope()) 2{ 3 atd.AssignIntVarValue(1); 4 Console.WriteLine("事务失败!"); 5 //scope1.Complete(); 6 Console.WriteLine("退出区域之前,i的值为:" + atd.i.ToString()); 7} |
1using (TransactionScope scope1 = new TransactionScope()) 2{ 3 atd.AssignIntVarValue(1); 4 atd.AssignIntVarValue(2); 5 Console.WriteLine("事务失败!"); 6 //scope1.Complete(); 7 Console.WriteLine("退出区域之前,i的值为:" + atd.i.ToString()); 8} |
1void IEnlistmentNotification.Rollback(Enlistment enlistment) 2{ 3 while (_var.i != _newValue) 4 { 5 Thread.Sleep(500); 6 } 7 _var.i = _oldValue; 8 Console.WriteLine("回滚!i的值变为:" + _oldValue.ToString()); 9 enlistment.Done(); 10} |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者