Example usage for org.springframework.transaction.interceptor RuleBasedTransactionAttribute setReadOnly

List of usage examples for org.springframework.transaction.interceptor RuleBasedTransactionAttribute setReadOnly

Introduction

In this page you can find the example usage for org.springframework.transaction.interceptor RuleBasedTransactionAttribute setReadOnly.

Prototype

public final void setReadOnly(boolean readOnly) 

Source Link

Document

Set whether to optimize as read-only transaction.

Usage

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

/**
 * //from   ww  w .ja  v  a2  s  . com
 * {@inheritDoc}
 */
@Override
public void process(final Object entry) throws Throwable {
    try {
        try {
            super.doProcess(entry);
        } 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_MANDATORY);

        final TransactionStatus transaction = this.txnManager.getTransaction(transactionAttribute);
        if (!transaction.isRollbackOnly()) {
            LOGGER.debug("Marking transaction as rollback-only due to exception during batch processing", ex);
            transaction.setRollbackOnly();
        }

        throw ex;
    }
}

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

/**
 * //from  ww w. jav a 2  s  .com
 * {@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:org.nabucco.alfresco.enhScriptEnv.repo.script.batch.RepositoryExecuteBatchWorker.java

/**
 * //from w ww . j a v a  2s .c  om
 * {@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:eap.config.TxAdviceBeanDefinitionParser.java

private RootBeanDefinition parseAttributeSource(Element attrEle, ParserContext parserContext) {
    List<Element> methods = DomUtils.getChildElementsByTagName(attrEle, METHOD_ELEMENT);
    ManagedMap<TypedStringValue, RuleBasedTransactionAttribute> transactionAttributeMap = new ManagedMap<TypedStringValue, RuleBasedTransactionAttribute>(
            methods.size());/*w w  w .j a v  a2  s  .co m*/
    transactionAttributeMap.setSource(parserContext.extractSource(attrEle));

    for (Element methodEle : methods) {
        String name = methodEle.getAttribute(METHOD_NAME_ATTRIBUTE);
        TypedStringValue nameHolder = new TypedStringValue(name);
        nameHolder.setSource(parserContext.extractSource(methodEle));

        RuleBasedTransactionAttribute attribute = new RuleBasedTransactionAttribute();
        String propagation = methodEle.getAttribute(PROPAGATION_ATTRIBUTE);
        String isolation = methodEle.getAttribute(ISOLATION_ATTRIBUTE);
        String timeout = methodEle.getAttribute(TIMEOUT_ATTRIBUTE);
        String readOnly = methodEle.getAttribute(READ_ONLY_ATTRIBUTE);
        if (StringUtils.hasText(propagation)) {
            attribute
                    .setPropagationBehaviorName(RuleBasedTransactionAttribute.PREFIX_PROPAGATION + propagation);
        }
        if (StringUtils.hasText(isolation)) {
            attribute.setIsolationLevelName(RuleBasedTransactionAttribute.PREFIX_ISOLATION + isolation);
        }
        if (StringUtils.hasText(timeout)) {
            try {
                attribute.setTimeout(Integer.parseInt(timeout));
            } catch (NumberFormatException ex) {
                parserContext.getReaderContext().error("Timeout must be an integer value: [" + timeout + "]",
                        methodEle);
            }
        }
        if (StringUtils.hasText(readOnly)) {
            attribute.setReadOnly(Boolean.valueOf(methodEle.getAttribute(READ_ONLY_ATTRIBUTE)));
        }

        List<RollbackRuleAttribute> rollbackRules = new LinkedList<RollbackRuleAttribute>();
        if (methodEle.hasAttribute(ROLLBACK_FOR_ATTRIBUTE)) {
            String rollbackForValue = methodEle.getAttribute(ROLLBACK_FOR_ATTRIBUTE);
            addRollbackRuleAttributesTo(rollbackRules, rollbackForValue);
        }
        if (methodEle.hasAttribute(NO_ROLLBACK_FOR_ATTRIBUTE)) {
            String noRollbackForValue = methodEle.getAttribute(NO_ROLLBACK_FOR_ATTRIBUTE);
            addNoRollbackRuleAttributesTo(rollbackRules, noRollbackForValue);
        }
        attribute.setRollbackRules(rollbackRules);

        transactionAttributeMap.put(nameHolder, attribute);
    }

    RootBeanDefinition attributeSourceDefinition = new RootBeanDefinition(
            NameMatchTransactionAttributeSource.class);
    attributeSourceDefinition.setSource(parserContext.extractSource(attrEle));
    attributeSourceDefinition.getPropertyValues().add("nameMap", transactionAttributeMap);
    return attributeSourceDefinition;
}