public override T Execute <T>(CommandConfig config, ICommand <T> command)
        {
            ICommandContext commandContext = Context.CommandContext;
            // Storing it in a variable, to reference later (it can change during command execution)
            bool isReused = commandContext.Reused;

            try
            {
                if (transactionContextFactory != null && !isReused)
                {
                    ITransactionContext transactionContext = transactionContextFactory.OpenTransactionContext(commandContext);
                    Context.TransactionContext = transactionContext;
                    commandContext.AddCloseListener(new TransactionCommandContextCloseListener(transactionContext));
                }

                return(next.Execute(config, command));
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                if (transactionContextFactory != null && !isReused)
                {
                    Context.RemoveTransactionContext();
                }
            }
        }
Пример #2
0
        public override T Execute <T>(CommandConfig config, ICommand <T> command)
        {
            long waitTime       = waitTimeInMs;
            int  failedAttempts = 0;

            do
            {
                if (failedAttempts > 0)
                {
                    log.LogInformation($"Waiting for {waitTime}ms before retrying the command.");
                    WaitBeforeRetry(waitTime);
                    waitTime *= waitIncreaseFactor;
                }

                try
                {
                    // try to execute the command
                    return(next.Execute(config, command));
                }
                catch (ActivitiOptimisticLockingException e)
                {
                    log.LogInformation("Caught optimistic locking exception: " + e);
                }

                failedAttempts++;
            } while (failedAttempts <= numOfRetries);

            throw new ActivitiException(numOfRetries + " retries failed with ActivitiOptimisticLockingException. Giving up.");
        }
Пример #3
0
        public virtual CommandConfig TransactionRequired()
        {
            CommandConfig config = new CommandConfig(this)
            {
                propagation = TransactionPropagation.REQUIRED
            };

            return(config);
        }
Пример #4
0
        public virtual CommandConfig SetContextReusePossible(bool contextReusePossible)
        {
            CommandConfig config = new CommandConfig(this)
            {
                contextReusePossible = contextReusePossible
            };

            return(config);
        }
Пример #5
0
        public virtual CommandConfig TransactionNotSupported()
        {
            CommandConfig config = new CommandConfig
            {
                contextReusePossible = false,
                propagation          = TransactionPropagation.NOT_SUPPORTED
            };

            return(config);
        }
Пример #6
0
        public virtual CommandConfig TransactionRequiresNew()
        {
            CommandConfig config = new CommandConfig
            {
                contextReusePossible = false,
                propagation          = TransactionPropagation.REQUIRES_NEW
            };

            return(config);
        }
Пример #7
0
        public override T Execute <T>(CommandConfig config, ICommand <T> command)
        {
            ICommandContext context = Context.CommandContext;

            bool contextReused = false;

            // We need to check the exception, because the transaction can be in a
            // rollback state, and some other command is being fired to compensate (eg. decrementing job retries)
            if (!config.ContextReusePossible || context == null || context.Exception != null)
            {
                context = commandContextFactory.CreateCommandContext <T>(command);
            }
            else
            {
                log.LogDebug($"Valid context found. Reusing it for the current command '{command.GetType().FullName}'");
                contextReused  = true;
                context.Reused = true;
            }

            try
            {
                // Push on stack
                Context.CommandContext             = context;
                Context.ProcessEngineConfiguration = processEngineConfiguration;

                return(next.Execute(config, command));
            }
            catch (NullReferenceException e)
            {
                context.SetException(e);
            }
            catch (Exception e)
            {
                context.SetException(e);
            }
            finally
            {
                try
                {
                    if (!contextReused)
                    {
                        context.Close();
                    }
                }
                finally
                {
                    // Pop from stack
                    Context.RemoveCommandContext();
                    Context.RemoveProcessEngineConfiguration();
                    Context.RemoveBpmnOverrideContext();
                }
            }

            return(default);
Пример #8
0
 public override T execute <T>(CommandConfig config, ICommand <T> command)
 {
     if (calledInsideTransaction())
     {
         //log.trace("Called inside transaction, skipping the retry interceptor.");
         return(next.execute(config, command));
     }
     else
     {
         return(base.execute(config, command));
     }
 }
Пример #9
0
        public override T execute <T>(CommandConfig config, ICommand <T> command)
        {
            //LOGGER.debug("Running command with propagation {}", config.TransactionPropagation);

            if (config.TransactionPropagation == TransactionPropagation.NOT_SUPPORTED)
            {
                return(next.execute(config, command));
            }

            bool        requiresNew = config.TransactionPropagation == TransactionPropagation.REQUIRES_NEW;
            Transaction oldTx       = null;

            try
            {
                bool existing = Existing;
                bool isNew    = !existing || requiresNew;
                if (existing && requiresNew)
                {
                    oldTx = doSuspend();
                }
                if (isNew)
                {
                    doBegin();
                }
                T result;
                try
                {
                    result = next.execute(config, command);
                }
                catch (Exception ex)
                {
                    doRollback(isNew, ex);
                    throw new Exception("TransactionCallback threw undeclared checked exception", ex);
                }
                if (isNew)
                {
                    doCommit();
                }
                return(result);
            }
            finally
            {
                doResume(oldTx);
            }
        }
Пример #10
0
        public override T Execute <T>(CommandConfig config, ICommand <T> command)
        {
            if (!log.IsEnabled(LogLevel.Debug))
            {
                // do nothing here if we cannot log
                return(next.Execute(config, command));
            }

            log.LogDebug("\n");
            log.LogDebug($"--- starting {command.GetType().Name} --------------------------------------------------------");
            try
            {
                return(next.Execute(config, command));
            }
            finally
            {
                log.LogDebug($"--- {command.GetType().Name} finished --------------------------------------------------------");
                log.LogDebug("\n");
            }
        }
Пример #11
0
        public override T Execute <T>(CommandConfig config, ICommand <T> command)
        {
            object value;

            ICommandContext commandContext = Context.CommandContext;

            try
            {
                // Execute the command.
                // This will produce operations that will be put on the agenda.
                commandContext.Agenda.PlanOperation(new RunableOperation(() =>
                {
                    T result = command.Execute(commandContext);

                    commandContext.SetResult(result);
                }));

                // Run loop for agenda
                ExecuteOperations(commandContext);

                // At the end, call the execution tree change listeners.
                // TODO: optimization: only do this when the tree has actually changed (ie check dbSqlSession).
                if (commandContext.HasInvolvedExecutions())
                {
                    Context.Agenda.PlanExecuteInactiveBehaviorsOperation();
                    ExecuteOperations(commandContext);
                }

                value = commandContext.GetResult();

                return((T)value);
            }
            catch (InvalidCastException ex)
            {
                System.Diagnostics.Debugger.Break();
                return(default);
Пример #12
0
 protected internal CommandConfig(CommandConfig commandConfig)
 {
     this.contextReusePossible = commandConfig.contextReusePossible;
     this.propagation          = commandConfig.propagation;
 }