Example usage for org.springframework.transaction TransactionDefinition PROPAGATION_SUPPORTS

List of usage examples for org.springframework.transaction TransactionDefinition PROPAGATION_SUPPORTS

Introduction

In this page you can find the example usage for org.springframework.transaction TransactionDefinition PROPAGATION_SUPPORTS.

Prototype

int PROPAGATION_SUPPORTS

To view the source code for org.springframework.transaction TransactionDefinition PROPAGATION_SUPPORTS.

Click Source Link

Document

Support a current transaction; execute non-transactionally if none exists.

Usage

From source file:org.nabucco.alfresco.enhScriptEnv.repo.script.batch.RepositoryExecuteBatchWorker.java

/**
 * //from w  w  w.  j ava 2s.c o  m
 * {@inheritDoc}
 */
@Override
public void beforeProcess() throws Throwable {
    // prepare execution context
    AuthenticationUtil.pushAuthentication();
    AuthenticationUtil.setFullyAuthenticatedUser(this.fullyAuthenticatedUser);
    if (this.runAsUser != null && !this.runAsUser.equals(this.fullyAuthenticatedUser)) {
        AuthenticationUtil.setRunAsUser(this.runAsUser);
    }

    I18NUtil.setLocale(this.locale);
    if (this.contentLocale != null) {
        I18NUtil.setContentLocale(this.contentLocale);
    }

    try {
        try {
            super.doBeforeProcess();
        } catch (final WrappedException ex) {
            // super should already handle unwrap runtime exceptions
            final Throwable wrappedException = ex.getWrappedException();
            if (wrappedException instanceof RuntimeException) {
                // super should have handled this
                throw (RuntimeException) wrappedException;
            }
            throw new AlfrescoRuntimeException(wrappedException.getMessage(), wrappedException);
        } catch (final Throwable ex) {
            throw new AlfrescoRuntimeException(ex.getMessage(), ex);
        }
    } catch (final Throwable ex) {
        /*
         * The TxnCallback does not propagate non-retryable exceptions to the retrying transaction handler. Some exceptions may be
         * caused by execution of the provided script callback without passing through a service with its transaction interceptor which
         * would mark the transaction for rollback. We have to mark the transaction for rollback manually otherwise we end up with
         * commits of partial changes from the batch. (rollback on any exception is the default behaviour of Alfresco
         * SpringAwareUserTransaction)
         */

        final RuleBasedTransactionAttribute transactionAttribute = new RuleBasedTransactionAttribute();
        transactionAttribute.setReadOnly(true);
        transactionAttribute.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);

        // this never creates a new "real" transaction due to our propagation behavior
        final TransactionStatus transaction = this.txnManager.getTransaction(transactionAttribute);
        try {
            if (!transaction.isRollbackOnly()) {
                LOGGER.debug("Marking transaction as rollback-only due to exception during batch processing",
                        ex);
                transaction.setRollbackOnly();
            }
        } finally {
            // this never actually commits a "real" transaction - it just clears transaction synchronizations
            this.txnManager.commit(transaction);
        }

        throw ex;
    }
}

From source file:org.nabucco.alfresco.enhScriptEnv.repo.script.batch.RepositoryExecuteBatchWorker.java

/**
 * /*  w  ww. ja  v  a 2s  . co  m*/
 * {@inheritDoc}
 */
@Override
public void afterProcess() throws Throwable {
    try {
        try {
            super.doAfterProcess();
        } catch (final WrappedException ex) {
            // super should already handle unwrap runtime exceptions
            final Throwable wrappedException = ex.getWrappedException();
            if (wrappedException instanceof RuntimeException) {
                // super should have handled this
                throw (RuntimeException) wrappedException;
            }
            throw new AlfrescoRuntimeException(wrappedException.getMessage(), wrappedException);
        } catch (final Throwable ex) {
            throw new AlfrescoRuntimeException(ex.getMessage(), ex);
        } finally {
            // cleanup execution context
            AuthenticationUtil.clearCurrentSecurityContext();
            AuthenticationUtil.popAuthentication();

            I18NUtil.setLocale(null);
            I18NUtil.setContentLocale(null);
        }
    } catch (final Throwable ex) {
        /*
         * The TxnCallback does not propagate non-retryable exceptions to the retrying transaction handler. Some exceptions may be
         * caused by execution of the provided script callback without passing through a service with its transaction interceptor which
         * would mark the transaction for rollback. We have to mark the transaction for rollback manually otherwise we end up with
         * commits of partial changes from the batch. (rollback on any exception is the default behaviour of Alfresco
         * SpringAwareUserTransaction)
         */

        final RuleBasedTransactionAttribute transactionAttribute = new RuleBasedTransactionAttribute();
        transactionAttribute.setReadOnly(true);
        transactionAttribute.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);

        // this never creates a new "real" transaction due to our propagation behavior
        final TransactionStatus transaction = this.txnManager.getTransaction(transactionAttribute);
        try {
            if (!transaction.isRollbackOnly()) {
                LOGGER.debug("Marking transaction as rollback-only due to exception during batch processing",
                        ex);
                transaction.setRollbackOnly();
            }
        } finally {
            // this never actually commits a "real" transaction - it just clears transaction synchronizations
            this.txnManager.commit(transaction);
        }

        throw ex;
    }
}

From source file:com._4dconcept.springframework.data.marklogic.datasource.DatabaseConnectorJtaTransactionTest.java

private void doTestJtaTransactionCommitWithNewTransactionWithinEmptyTransaction(final boolean requiresNew,
        boolean notSupported) throws Exception {

    if (notSupported) {
        given(userTransaction.getStatus()).willReturn(Status.STATUS_ACTIVE, Status.STATUS_NO_TRANSACTION,
                Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
        given(transactionManager.suspend()).willReturn(transaction);
    } else {/*from  ww  w . j ava  2  s.  c o m*/
        given(userTransaction.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION,
                Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    }

    final ContentSource contentSource = mock(ContentSource.class);
    final Session session1 = mock(Session.class);
    final Session session2 = mock(Session.class);
    given(contentSource.newSession()).willReturn(session1, session2);

    final JtaTransactionManager ptm = new JtaTransactionManager(userTransaction, transactionManager);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(notSupported ? TransactionDefinition.PROPAGATION_NOT_SUPPORTED
            : TransactionDefinition.PROPAGATION_SUPPORTS);

    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
            assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
            assertSame(session1, ContentSourceUtils.getSession(contentSource));
            assertSame(session1, ContentSourceUtils.getSession(contentSource));

            TransactionTemplate tt2 = new TransactionTemplate(ptm);
            tt2.setPropagationBehavior(requiresNew ? TransactionDefinition.PROPAGATION_REQUIRES_NEW
                    : TransactionDefinition.PROPAGATION_REQUIRED);
            tt2.execute(new TransactionCallbackWithoutResult() {
                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
                    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
                    assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
                    assertSame(session2, ContentSourceUtils.getSession(contentSource));
                    assertSame(session2, ContentSourceUtils.getSession(contentSource));
                }
            });

            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
            assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
            assertSame(session1, ContentSourceUtils.getSession(contentSource));
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    verify(userTransaction).begin();
    verify(userTransaction).commit();
    if (notSupported) {
        verify(transactionManager).resume(transaction);
    }
    verify(session2).close();
    verify(session1).close();
}

From source file:nl.nn.adapterframework.core.PipeLine.java

public void setTransacted(boolean transacted) {
    //      this.transacted = transacted;
    ConfigurationWarnings configWarnings = ConfigurationWarnings.getInstance();
    if (transacted) {
        String msg = "Pipeline of [" + owner.getName()
                + "] implementing setting of transacted=true as transactionAttribute=Required";
        configWarnings.add(log, msg);//w  w  w. ja v  a 2s. c o m
        setTransactionAttributeNum(TransactionDefinition.PROPAGATION_REQUIRED);
    } else {
        String msg = "Pipeline of [" + owner.getName()
                + "] implementing setting of transacted=false as transactionAttribute=Supports";
        configWarnings.add(log, msg);
        setTransactionAttributeNum(TransactionDefinition.PROPAGATION_SUPPORTS);
    }
}

From source file:nl.nn.adapterframework.receivers.ReceiverBase.java

/**
 * Controls the use of XA-transactions./*from  w  ww .j  av  a2s  .  c o  m*/
 */
public void setTransacted(boolean transacted) {
    //      this.transacted = transacted;
    ConfigurationWarnings configWarnings = ConfigurationWarnings.getInstance();
    if (transacted) {
        String msg = getLogPrefix()
                + "implementing setting of transacted=true as transactionAttribute=Required";
        configWarnings.add(log, msg);
        setTransactionAttributeNum(TransactionDefinition.PROPAGATION_REQUIRED);
    } else {
        String msg = getLogPrefix()
                + "implementing setting of transacted=false as transactionAttribute=Supports";
        configWarnings.add(log, msg);
        setTransactionAttributeNum(TransactionDefinition.PROPAGATION_SUPPORTS);
    }
}