Exemplo n.º 1
0
 protected internal virtual void CloseSessions(CommandInvocationContext commandInvocationContext)
 {
     //Scope.Dispose();
     //Log.LogInfo("销毁DI对象","------------------Dispose CommandContext Scope------------------");
     //foreach (var session in SessionList)
     //    try
     //    {
     //        session.Close();
     //    }
     //    catch (System.Exception exception)
     //    {
     //        commandInvocationContext.TrySetThrowable(exception);
     //    }
 }
Exemplo n.º 2
0
        public override T Execute <T>(ICommand <T> command)
        {
            CommandContext context = null;

            if (!AlwaysOpenNew)
            {
                // check whether we can reuse the command context
                var existingCommandContext = Context.CommandContext;
                if ((existingCommandContext != null) && IsFromSameEngine(existingCommandContext))
                {
                    context = existingCommandContext;
                }
            }
            var openNew    = context == null;
            var commandObj = command as ICommand <object>;
            var commandInvocationContext = new CommandInvocationContext(commandObj);

            Context.CommandInvocationContext = commandInvocationContext;

            try
            {
                if (openNew)
                {
                    Log.DebugOpeningNewCommandContext();
                    context = CommandContextFactory.CreateCommandContext();
                }
                else
                {
                    Log.DebugReusingExistingCommandContext();
                }

                Context.CommandContext             = context;
                Context.ProcessEngineConfiguration = ProcessEngineConfiguration;

                // delegate to next interceptor in chain
                return(Next.Execute(command));
            }
            //// 取消catch 方便debug
            catch (System.Exception e)
            {
                commandInvocationContext.TrySetThrowable(e);
                throw;
            }
            finally
            {
                try
                {
                    if (openNew)
                    {
                        Log.ClosingCommandContext();
                        context.Close(commandInvocationContext);
                    }
                    else
                    {
                        commandInvocationContext.Rethrow();
                    }
                }
                finally
                {
                    Context.RemoveCommandInvocationContext();
                    Context.RemoveCommandContext();
                    Context.RemoveProcessEngineConfiguration();
                }
            }
        }
Exemplo n.º 3
0
        public virtual void Close(CommandInvocationContext commandInvocationContext)
        {
            // the intention of this method is that all resources are closed properly,
            // even
            // if exceptions occur in close or flush methods of the sessions or the
            // transaction context.

            //try
            //{
            //    try
            //    {
            //        try
            //        {
            if (commandInvocationContext.Throwable == null)
            {
                FireCommandContextClose();
                FlushSessions();
                //清理缓存
                Context.CommandContext.DbEntityCache.Clear();
            }
            //}
            //catch (System.Exception exception)
            //{
            //    commandInvocationContext.TrySetThrowable(exception);
            //}
            //finally
            //{
            //try
            //{
            if (commandInvocationContext.Throwable == null)
            {
                FlushTransactions();//外部代码事务
                try
                {
                    transactionContext.Commit(); //系统数据库事务
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    EntityEntry entry = ex.Entries.ElementAt(0);
                    var         dbOp  = new DbEntityOperation();
                    dbOp.SetFailed(false);
                    dbOp.EntityType = entry.Entity.GetType();
                    dbOp.Entity     = entry.Entity as IDbEntity;
                    HandleOptimisticLockingException(dbOp);
                }
            }

            //}
            //catch (System.Exception exception)
            //{
            //    commandInvocationContext.TrySetThrowable(exception);
            //}

            //if (commandInvocationContext.Throwable != null)
            //{
            //    // fire command failed (must not fail itself)
            //    FireCommandFailed(commandInvocationContext.Throwable);

            //    if (ShouldLogInfo(commandInvocationContext.Throwable))
            //        Log.InfoException(commandInvocationContext.Throwable);
            //    else if (ShouldLogFine(commandInvocationContext.Throwable))
            //        Log.DebugException(commandInvocationContext.Throwable);
            //    else
            //        Log.ErrorException(commandInvocationContext.Throwable);
            //    transactionContext.Rollback();
            //}
            //}
            //}
            //catch (System.Exception exception)
            //{
            //    commandInvocationContext.TrySetThrowable(exception);
            //}
            //finally
            //{
            CloseSessions(commandInvocationContext);
            //}
            //}
            //catch (System.Exception exception)
            //{
            //    commandInvocationContext.TrySetThrowable(exception);
            //}

            // rethrow the original exception if there was one
            //commandInvocationContext.Rethrow();
        }