static async Task TransactionScopeAsync() { using (var scope = new TransactionScope()) { Transaction.Current.TransactionCompleted += OnTransactionCompleted; TransactionUtilities.DisplayTransactionInformation("Ambient TX created", Transaction.Current.TransactionInformation); var s1 = new Student { FirstName = "Angela", LastName = "Nagel", Company = "Kantine M101" }; var db = new StudentData(); await db.AddStudentAsync(s1); if (!TransactionUtilities.AbortTransaction()) { scope.Complete(); } else { Console.WriteLine("transaction will be aborted"); } } // scope.Dispose() }
/// <summary> /// Open the given objectId ForRead inside of a new transaction and then call the specified function; /// the transaction will be Commit()ted. /// </summary> public static TResult Use <TDBObject, TResult>(this ObjectId <TDBObject> id, Func <TDBObject, TResult> f) where TDBObject : DBObject { lock (Runtime.DBSyncObject) { return(TransactionUtilities.UseId(id, OpenMode.ForRead, f)); } }
static async Task CommittableTransactionAsync() { var tx = new CommittableTransaction(); TransactionUtilities.DisplayTransactionInformation("TX created", tx.TransactionInformation); try { var s1 = new Student { FirstName = "Stephanie", LastName = "Nagel", Company = "CN innovation" }; var db = new StudentData(); await db.AddStudentAsync(s1, tx); if (TransactionUtilities.AbortTransaction()) { throw new ApplicationException("transaction abort"); } tx.Commit(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(); tx.Rollback(); } TransactionUtilities.DisplayTransactionInformation("TX completed", tx.TransactionInformation); }
static void DependentTransaction() { var tx = new CommittableTransaction(); TransactionUtilities.DisplayTransactionInformation("Root TX created", tx.TransactionInformation); try { Task.Factory.StartNew(TxTask, tx.DependentClone(DependentCloneOption.BlockCommitUntilComplete)); if (TransactionUtilities.AbortTransaction()) { throw new ApplicationException("transaction abort"); } tx.Commit(); } catch (Exception ex) { Console.WriteLine(ex.Message); tx.Rollback(); } TransactionUtilities.DisplayTransactionInformation("TX finished", tx.TransactionInformation); }
/// <summary> /// 重置未提醒扫描状态:正在扫描=>等待扫描 /// </summary> /// <returns></returns> public bool ResetNotNoticeScanningToWait() { try { using (var scope = TransactionUtilities.CreateTransactionScopeWithNoLock()) { _userInfoRepository.ChangeScanningToWait(); scope.Complete(); return(true); } } catch (Exception ex) { return(false); } }
static void TxTask(object obj) { var tx = obj as DependentTransaction; if (tx == null) { return; } TransactionUtilities.DisplayTransactionInformation("Dependent Transaction", tx.TransactionInformation); Thread.Sleep(3000); tx.Complete(); TransactionUtilities.DisplayTransactionInformation("Dependent TX Complete", tx.TransactionInformation); }
static void NestedScopes() { using (var scope = new TransactionScope()) { Transaction.Current.TransactionCompleted += OnTransactionCompleted; TransactionUtilities.DisplayTransactionInformation("Ambient TX created", Transaction.Current.TransactionInformation); using (var scope2 = new TransactionScope(TransactionScopeOption.RequiresNew)) { Transaction.Current.TransactionCompleted += OnTransactionCompleted; TransactionUtilities.DisplayTransactionInformation( "Inner Transaction Scope", Transaction.Current.TransactionInformation); scope2.Complete(); } scope.Complete(); } }
/// <summary> /// 提交未提醒 /// </summary> /// <param name="strId"></param> public void PostNotNotice(string strId) { var entity = new UserInfo(); try { using (var scope = TransactionUtilities.CreateTransactionScopeWithNoLock()) { var id = int.Parse(strId); entity = _userInfoRepository.GetByCondition( new DirectSpecification <UserInfo>( x => x.ID == id)); entity.ScanFlag = 3; //entity.NoticeTime = DateTime.Now; base.Update(entity, x => x.ScanFlag); scope.Complete(); } } catch (InvalidOperationException e) { lock (_myLock) { LoggerHelper.Log("【发起提醒】失败,失败原因:" + (e.InnerException == null ? e.Message : e.InnerException.ToString())); } } catch (Exception ex) { lock (_myLock) { LoggerHelper.Log("【未提醒】失败,失败原因:" + (ex.InnerException == null ? ex.Message : ex.InnerException.ToString())); //避免数据库异常下,无法记录错误日志 entity.ScanFlag = 3; base.Update(entity, x => x.ScanFlag); } } }
/// <summary> /// Open the given objectId ForWrite inside of a new transaction and then call the specified function; /// the transaction will be Commit()ted. /// </summary> public static TResult UseForWrite <TDBObject, TResult>(this ObjectId <TDBObject> id, Func <TDBObject, TResult> f) where TDBObject : DBObject { return(TransactionUtilities.UseId(id, OpenMode.ForWrite, f)); }
static void OnTransactionCompleted(object sender, TransactionEventArgs e) { TransactionUtilities.DisplayTransactionInformation("TX completed", e.Transaction.TransactionInformation); }