Search in sources :

Example 1 with TransactionListener

use of org.activiti.engine.impl.cfg.TransactionListener in project Activiti by Activiti.

the class JobEntityManager method hintJobExecutor.

protected void hintJobExecutor(JobEntity job) {
    JobExecutor jobExecutor = Context.getProcessEngineConfiguration().getJobExecutor();
    // notify job executor:      
    TransactionListener transactionListener = new JobAddedNotification(jobExecutor);
    Context.getCommandContext().getTransactionContext().addTransactionListener(TransactionState.COMMITTED, transactionListener);
}
Also used : TransactionListener(org.activiti.engine.impl.cfg.TransactionListener) JobExecutor(org.activiti.engine.impl.jobexecutor.JobExecutor) JobAddedNotification(org.activiti.engine.impl.jobexecutor.JobAddedNotification) AsyncJobAddedNotification(org.activiti.engine.impl.jobexecutor.AsyncJobAddedNotification)

Example 2 with TransactionListener

use of org.activiti.engine.impl.cfg.TransactionListener in project Activiti by Activiti.

the class EntityManagerSessionImpl method getEntityManager.

public EntityManager getEntityManager() {
    if (entityManager == null) {
        entityManager = getEntityManagerFactory().createEntityManager();
        if (handleTransactions) {
            // Add transaction listeners, if transactions should be handled
            TransactionListener jpaTransactionCommitListener = new TransactionListener() {

                public void execute(CommandContext commandContext) {
                    if (isTransactionActive()) {
                        entityManager.getTransaction().commit();
                    }
                }
            };
            TransactionListener jpaTransactionRollbackListener = new TransactionListener() {

                public void execute(CommandContext commandContext) {
                    if (isTransactionActive()) {
                        entityManager.getTransaction().rollback();
                    }
                }
            };
            TransactionContext transactionContext = Context.getTransactionContext();
            transactionContext.addTransactionListener(TransactionState.COMMITTED, jpaTransactionCommitListener);
            transactionContext.addTransactionListener(TransactionState.ROLLED_BACK, jpaTransactionRollbackListener);
            // Also, start a transaction, if one isn't started already
            if (!isTransactionActive()) {
                entityManager.getTransaction().begin();
            }
        }
    }
    return entityManager;
}
Also used : TransactionListener(org.activiti.engine.impl.cfg.TransactionListener) CommandContext(org.activiti.engine.impl.interceptor.CommandContext) TransactionContext(org.activiti.engine.impl.cfg.TransactionContext)

Example 3 with TransactionListener

use of org.activiti.engine.impl.cfg.TransactionListener in project Activiti by Activiti.

the class StandaloneMybatisTransactionContext method fireTransactionEvent.

/**
 * Fires the event for the provided {@link TransactionState}.
 *
 * @param transactionState The {@link TransactionState} for which the listeners will be called.
 * @param executeInNewContext If true, the listeners will be called in a new command context.
 *                            This is needed for example when firing the {@link TransactionState#COMMITTED}
 *                            event: the transacation is already committed and executing logic in the same
 *                            context could lead to strange behaviour (for example doing a {@link SqlSession#update(String)}
 *                            would actually roll back the update (as the MyBatis context is already committed
 *                            and the internal flags have not been correctly set).
 */
protected void fireTransactionEvent(TransactionState transactionState, boolean executeInNewContext) {
    if (stateTransactionListeners == null) {
        return;
    }
    final List<TransactionListener> transactionListeners = stateTransactionListeners.get(transactionState);
    if (transactionListeners == null) {
        return;
    }
    if (executeInNewContext) {
        CommandExecutor commandExecutor = commandContext.getProcessEngineConfiguration().getCommandExecutor();
        CommandConfig commandConfig = new CommandConfig(false, TransactionPropagation.REQUIRES_NEW);
        commandExecutor.execute(commandConfig, new Command<Void>() {

            public Void execute(CommandContext commandContext) {
                executeTransactionListeners(transactionListeners, commandContext);
                return null;
            }
        });
    } else {
        executeTransactionListeners(transactionListeners, commandContext);
    }
}
Also used : TransactionListener(org.activiti.engine.impl.cfg.TransactionListener) CommandConfig(org.activiti.engine.impl.interceptor.CommandConfig) CommandContext(org.activiti.engine.impl.interceptor.CommandContext) CommandExecutor(org.activiti.engine.impl.interceptor.CommandExecutor)

Example 4 with TransactionListener

use of org.activiti.engine.impl.cfg.TransactionListener in project Activiti by Activiti.

the class JobEntityManager method hintAsyncExecutor.

protected void hintAsyncExecutor(JobEntity job) {
    AsyncExecutor asyncExecutor = Context.getProcessEngineConfiguration().getAsyncExecutor();
    // notify job executor:      
    TransactionListener transactionListener = new AsyncJobAddedNotification(job, asyncExecutor);
    Context.getCommandContext().getTransactionContext().addTransactionListener(TransactionState.COMMITTED, transactionListener);
}
Also used : TransactionListener(org.activiti.engine.impl.cfg.TransactionListener) AsyncJobAddedNotification(org.activiti.engine.impl.jobexecutor.AsyncJobAddedNotification) AsyncExecutor(org.activiti.engine.impl.asyncexecutor.AsyncExecutor)

Aggregations

TransactionListener (org.activiti.engine.impl.cfg.TransactionListener)4 CommandContext (org.activiti.engine.impl.interceptor.CommandContext)2 AsyncJobAddedNotification (org.activiti.engine.impl.jobexecutor.AsyncJobAddedNotification)2 AsyncExecutor (org.activiti.engine.impl.asyncexecutor.AsyncExecutor)1 TransactionContext (org.activiti.engine.impl.cfg.TransactionContext)1 CommandConfig (org.activiti.engine.impl.interceptor.CommandConfig)1 CommandExecutor (org.activiti.engine.impl.interceptor.CommandExecutor)1 JobAddedNotification (org.activiti.engine.impl.jobexecutor.JobAddedNotification)1 JobExecutor (org.activiti.engine.impl.jobexecutor.JobExecutor)1