Exemplo n.º 1
0
        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()
        }
Exemplo n.º 2
0
 /// <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));
     }
 }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
 /// <summary>
 /// 重置未提醒扫描状态:正在扫描=>等待扫描
 /// </summary>
 /// <returns></returns>
 public bool ResetNotNoticeScanningToWait()
 {
     try
     {
         using (var scope = TransactionUtilities.CreateTransactionScopeWithNoLock())
         {
             _userInfoRepository.ChangeScanningToWait();
             scope.Complete();
             return(true);
         }
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        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();
            }
        }
Exemplo n.º 8
0
        /// <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);
                }
            }
        }
Exemplo n.º 9
0
 /// <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));
 }
Exemplo n.º 10
0
 static void OnTransactionCompleted(object sender, TransactionEventArgs e)
 {
     TransactionUtilities.DisplayTransactionInformation("TX completed",
                                                        e.Transaction.TransactionInformation);
 }