示例#1
0
 public static void Transaction(IsolationLevel level, Proc transactional, UnitOfWorkNestingOptions nestingOptions)
 {
     using (UnitOfWork.Start(nestingOptions))
     {
         // If we are already in a transaction, don't start a new one
         if (UnitOfWork.Current.IsInActiveTransaction)
         {
             transactional();
         }
         else
         {
             RhinoTransaction tx = UnitOfWork.Current.BeginTransaction(level);
             try
             {
                 transactional();
                 tx.Commit();
             }
             catch
             {
                 tx.Rollback();
                 throw;
             }
             finally
             {
                 tx.Dispose();
             }
         }
     }
 }
 public static void AutoRollbackTransaction(IsolationLevel level, Proc transactional, UnitOfWorkNestingOptions nestingOptions)
 {
     using (UnitOfWork.Start(nestingOptions))
     {
         // If we are already in a transaction, don't start a new one
         if (UnitOfWork.Current.IsInActiveTransaction)
         {
             transactional();
         }
         else
         {
             RhinoTransaction tx = UnitOfWork.Current.BeginTransaction(level);
             try
             {
                 transactional();
                 tx.Rollback();
             }
             catch
             {
                 tx.Rollback();
                 throw;
             }
             finally
             {
                 tx.Dispose();
             }
         }
     }
 }
示例#3
0
        /// <summary>
        /// 새로운 Unit Of Work을 시작합니다.
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="nestingOptions"></param>
        /// <returns></returns>
        public static IUnitOfWork Start(IDbConnection connection, UnitOfWorkNestingOptions nestingOptions)
        {
            if (IsDebugEnabled)
            {
                log.Debug("새로운 Unit Of Work 를 시작합니다. connection=[{0}], nestingOptions=[{1}]", connection, nestingOptions);
            }

            if (_globalNonThreadSafeUnitOfWork != null)
            {
                return(_globalNonThreadSafeUnitOfWork);
            }

            var existing = Local.Data[CurrentUnitOfWorkKey] as IUnitOfWorkImplementor;

            if (nestingOptions == UnitOfWorkNestingOptions.ReturnExistingOrCreateUnitOfWork && existing != null)
            {
                if (IsDebugEnabled)
                {
                    log.Debug("기존 IUnitOfWork가 존재하므로, 사용횟수만 증가시키고, 기존 인스턴스를 사용합니다.");
                }

                existing.IncrementUsages();
                return(existing);
            }

            if (IsDebugEnabled)
            {
                log.Debug("Castle.Windsor IoC를 통해 IUnitOfWorkFactory와 IUnitOfWork를 생성합니다.");
            }

            // NOTE : SQLite 메모리 DB에서는 현재 사용하고 있는 DB의 Connection을 이용해야 한다.
            //
            if (existing != null && connection == null && !(existing is NHMultipleUnitOfWorkImplementor))
            {
                // connection = ((ISessionFactoryImplementor) UnitOfWorkFactory.SessionFactory).ConnectionProvider.GetConnection();
                connection = existing.Session.Connection;
            }

            Current = UnitOfWorkFactory.Create(connection, existing);

            if (IsDebugEnabled)
            {
                log.Debug("새로운 UnitOfWork 를 시작했습니다. connection=[{0}]", connection);
            }

            return(Current);
        }
示例#4
0
        /// <summary>
        /// Start a Unit of Work
        /// is called
        /// </summary>
        /// <returns>
        /// An IUnitOfwork object that can be used to work with the current UoW.
        /// </returns>
        public static IUnitOfWork Start(IDbConnection connection, UnitOfWorkNestingOptions nestingOptions)
        {
            if (globalNonThreadSafeUnitOfwork != null)
            {
                return(globalNonThreadSafeUnitOfwork);
            }
            IUnitOfWorkImplementor existing = (IUnitOfWorkImplementor)Local.Data[CurrentUnitOfWorkKey];

            if (nestingOptions == UnitOfWorkNestingOptions.ReturnExistingOrCreateUnitOfWork &&
                existing != null)
            {
                existing.IncrementUsages();
                return(existing);
            }
            Current = IoC.Resolve <IUnitOfWorkFactory>().Create(connection, existing);
            foreach (IUnitOfWorkAware workAware in IoC.ResolveAll <IUnitOfWorkAware>())
            {
                workAware.UnitOfWorkStarted(Current);
            }
            return(Current);
        }
示例#5
0
        /// <summary>
        /// Start a Unit of Work
        /// is called
        /// </summary>
        /// <returns>
        /// An IUnitOfwork object that can be used to work with the current UoW.
        /// </returns>
        public static IUnitOfWork Start(UnitOfWorkNestingOptions nestingOptions)
        {
            if (_globalNonThreadSafeUnitOfwork != null)
            {
                return(_globalNonThreadSafeUnitOfwork);
            }

            UnitOfWorkAdapter unitOfWorkAdapter = (UnitOfWorkAdapter)ServerContext.Request[CurrentUnitOfWorkKey];

            if (nestingOptions == UnitOfWorkNestingOptions.ReturnExistingOrCreateUnitOfWork &&
                unitOfWorkAdapter != null)
            {
                unitOfWorkAdapter.IncremementUsages();
                return(unitOfWorkAdapter);
            }

            UnitOfWorkAdapter previous = unitOfWorkAdapter;

            unitOfWorkAdapter = ObjectBuilder.Get <UnitOfWorkAdapter>();
            unitOfWorkAdapter.SetPrevious(previous);

            return(unitOfWorkAdapter);
        }
示例#6
0
        /// <summary>
        /// 새로운 Unit Of Work을 시작합니다.
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="nestingOptions"></param>
        /// <returns></returns>
        public static IUnitOfWork Start(IDbConnection connection, UnitOfWorkNestingOptions nestingOptions) {
            if(IsDebugEnabled)
                log.Debug("새로운 Unit Of Work 를 시작합니다. connection=[{0}], nestingOptions=[{1}]", connection, nestingOptions);

            if(_globalNonThreadSafeUnitOfWork != null)
                return _globalNonThreadSafeUnitOfWork;

            var existing = Local.Data[CurrentUnitOfWorkKey] as IUnitOfWorkImplementor;

            if(nestingOptions == UnitOfWorkNestingOptions.ReturnExistingOrCreateUnitOfWork && existing != null) {
                if(IsDebugEnabled)
                    log.Debug("기존 IUnitOfWork가 존재하므로, 사용횟수만 증가시키고, 기존 인스턴스를 사용합니다.");

                existing.IncrementUsages();
                return existing;
            }

            if(IsDebugEnabled)
                log.Debug("Castle.Windsor IoC를 통해 IUnitOfWorkFactory와 IUnitOfWork를 생성합니다.");

            // NOTE : SQLite 메모리 DB에서는 현재 사용하고 있는 DB의 Connection을 이용해야 한다.
            //
            if(existing != null && connection == null && !(existing is NHMultipleUnitOfWorkImplementor))
                // connection = ((ISessionFactoryImplementor) UnitOfWorkFactory.SessionFactory).ConnectionProvider.GetConnection();
                connection = existing.Session.Connection;

            Current = UnitOfWorkFactory.Create(connection, existing);

            if(IsDebugEnabled)
                log.Debug("새로운 UnitOfWork 를 시작했습니다. connection=[{0}]", connection);

            return Current;
        }
示例#7
0
 /// <summary>
 /// 새로운 Unit Of Work을 시작합니다.
 /// </summary>
 /// <param name="nestingOptions"></param>
 /// <returns></returns>
 public static IUnitOfWork Start(UnitOfWorkNestingOptions nestingOptions) {
     return Start(null, nestingOptions);
 }
示例#8
0
 /// <summary>
 /// Start a Unit of Work
 /// is called
 /// </summary>
 /// <returns>
 /// An IUnitOfwork object that can be used to work with the current UoW.
 /// </returns>
 public static IUnitOfWork Start(IDbConnection connection, UnitOfWorkNestingOptions nestingOptions)
 {
     if (globalNonThreadSafeUnitOfwork != null)
         return globalNonThreadSafeUnitOfwork;
     IUnitOfWorkImplementor existing = (IUnitOfWorkImplementor)Local.Data[CurrentUnitOfWorkKey];
     if (nestingOptions == UnitOfWorkNestingOptions.ReturnExistingOrCreateUnitOfWork &&
         existing != null)
     {
         existing.IncrementUsages();
         return existing;
     }
     Current = IoC.Resolve<IUnitOfWorkFactory>().Create(connection, existing);
     foreach (IUnitOfWorkAware workAware in IoC.ResolveAll<IUnitOfWorkAware>())
         workAware.UnitOfWorkStarted(Current);
     return Current;
 }
示例#9
0
        /// <summary>
        ///  지정된 함수를 현재 UnitOfWork의 Transaction 하에서 수행합니다.
        /// </summary>
        /// <typeparam name="T">Return Type of Function to execute</typeparam>
        /// <param name="transactionalFunc">Function to execute under Transaction</param>
        /// <param name="isolationLevel">격리수준</param>
        /// <param name="nestingOptions">UnitOfWork Nesting option.</param>
        /// <returns>함수 수행 결과</returns>
        public static T Transaction <T>(Func <T> transactionalFunc, IsolationLevel isolationLevel, UnitOfWorkNestingOptions nestingOptions)
        {
            transactionalFunc.ShouldNotBeNull("transactionalFunc");

            if (IsDebugEnabled)
            {
                log.Debug("Execute the specified action under transaction... " +
                          "transactionalFunc=[{0}], isolationLevel=[{1}], nestingOptions=[{2}]",
                          transactionalFunc, isolationLevel, nestingOptions);
            }

            // 기존 UnitOfWork에 참여할 수도 있고, 새로운 UnitOfWork를 생성할 수도 있습니다.
            //
            using (UnitOfWork.Start(nestingOptions)) {
                // 기존 Transaction 이 없다면, 새로운 Transaction 에서 작업합니다.
                //
                if (UnitOfWork.Current.IsInActiveTransaction == false)
                {
                    if (IsDebugEnabled)
                    {
                        log.Debug("새로운 NHibernate.ITransaction을 생성합니다.");
                    }

                    var tx = UnitOfWork.Current.BeginTransaction(isolationLevel);

                    try {
                        var result = transactionalFunc();
                        tx.Commit();

                        if (IsDebugEnabled)
                        {
                            log.Debug("Transactional Function을 수행하고, Transaction.Commit을 수행하였습니다!!!");
                        }

                        return(result);
                    }
                    catch (Exception ex) {
                        if (log.IsErrorEnabled)
                        {
                            log.Error("Fail to execute transactional action. rollback transaction.");
                            log.Error(ex);
                        }

                        tx.Rollback();
                        throw;
                    }
                    finally {
                        if (tx != null)
                        {
                            tx.Dispose();

                            if (IsDebugEnabled)
                            {
                                log.Debug("Dispose current transaction.");
                            }
                        }
                    }
                }

                if (IsDebugEnabled)
                {
                    log.Debug("활성화된 기존 Transaction에 참여하여, 실행합니다.");
                }

                return(transactionalFunc.Invoke());
            }
        }
示例#10
0
        /// <summary>
        /// 지정된 <see cref="Action"/>를 지정된 격리수준의 Transaction 하에서 수행한다.
        /// </summary>
        /// <param name="isolationLevel">격리수준</param>
        /// <param name="transactionalAction">Action to execute under Transaction</param>
        /// <param name="nestingOptions">UnitOfWork Nesting option.</param>
        public static void Transaction(IsolationLevel isolationLevel,
                                       Action transactionalAction,
                                       UnitOfWorkNestingOptions nestingOptions)
        {
            transactionalAction.ShouldNotBeNull("transactionalAction");

            if (IsDebugEnabled)
            {
                log.Debug("Execute the specified action under transaction... " +
                          "isolationLevel=[{0}], transactionalAction=[{1}], nestingOptions=[{2}]",
                          isolationLevel, transactionalAction, nestingOptions);
            }

            using (UnitOfWork.Start(nestingOptions)) {
                // if we are already in a transaction, don't start a new one
                if (UnitOfWork.Current.IsInActiveTransaction)
                {
                    if (IsDebugEnabled)
                    {
                        log.Debug("활성화된 기존 Transaction에 참여합니다.");
                    }

                    transactionalAction();
                }
                else
                {
                    if (IsDebugEnabled)
                    {
                        log.Debug("새로운 NHibernate.ITransaction을 생성합니다.");
                    }

                    var tx = UnitOfWork.Current.BeginTransaction(isolationLevel);

                    try {
                        transactionalAction();

                        tx.Commit();

                        if (IsDebugEnabled)
                        {
                            log.Debug("Transactional Action을 수행하고, Transaction.Commit을 수행하였습니다.");
                        }
                    }
                    catch (Exception ex) {
                        if (log.IsErrorEnabled)
                        {
                            log.Error("Fail to execute transactional action. rollback transaction.");
                            log.Error(ex);
                        }

                        tx.Rollback();
                        throw;
                    }
                    finally {
                        if (tx != null)
                        {
                            tx.Dispose();

                            if (IsDebugEnabled)
                            {
                                log.Debug("Dispose current transaction.");
                            }
                        }
                    }
                }
            }
        }
示例#11
0
 public static IUnitOfWork Start(UnitOfWorkNestingOptions nestingOptions)
 {
     return(Start(null, nestingOptions));
 }