Example usage for org.springframework.transaction.interceptor TransactionAttributeSource getTransactionAttribute

List of usage examples for org.springframework.transaction.interceptor TransactionAttributeSource getTransactionAttribute

Introduction

In this page you can find the example usage for org.springframework.transaction.interceptor TransactionAttributeSource getTransactionAttribute.

Prototype

@Nullable
TransactionAttribute getTransactionAttribute(Method method, @Nullable Class<?> targetClass);

Source Link

Document

Return the transaction attribute for the given method, or null if the method is non-transactional.

Usage

From source file:net.tirasa.blog.ilgrosso.dynamicspringtransactional.DynamicTransactionInterceptor.java

@Override
public TransactionAttributeSource getTransactionAttributeSource() {
    final TransactionAttributeSource origTxAttrSource = super.getTransactionAttributeSource();

    return new TransactionAttributeSource() {

        @Override/*from ww  w  .  java 2 s . c o  m*/
        public TransactionAttribute getTransactionAttribute(final Method method, final Class<?> targetClass) {
            TransactionAttribute txAttr = origTxAttrSource.getTransactionAttribute(method, targetClass);

            if (txAttr instanceof DefaultTransactionAttribute) {
                ((DefaultTransactionAttribute) txAttr).setQualifier(AuthContextUtils.getDomain());
            }

            return txAttr;
        }
    };
}

From source file:com.newmainsoftech.spray.slingong.datastore.Slim3PlatformTransactionManagerTest.java

@Test
public void testTransactionalAttributesOnStaticMethod1() throws Throwable {
    try {/*w w  w.  j  av a 2  s  . co m*/
        Method createNewAuthorStaticMethod = Author.class.getDeclaredMethod("createNewAuthor",
                new Class[] { String.class });

        TransactionAttributeSource transactionAttributeSource
        //         = new AnnotationTransactionAttributeSource( false);
                = annotationTransactionAspect.getTransactionAttributeSource();

        TransactionAttribute transactionAttribute = transactionAttributeSource
                .getTransactionAttribute(createNewAuthorStaticMethod, Author.class);

        /* Made addition to ExtendedAnnotationTransactionAspect, so no matter of attribute value of 
         * @Transactional annotation, noRollBack method instead of rollback method of 
         * Slim3PlatformTransactionManager class will be called when encountering 
         * ConcurrentModificationException exception and DeadlineExceededException exception. 
        Assert.assertFalse( 
              transactionAttribute.rollbackOn( new ConcurrentModificationException()));
        Assert.assertFalse( 
              transactionAttribute.rollbackOn( new DeadlineExceededException()));
        */

        String name = transactionAttribute.getName();
        String message = String.format(
                "Attribution values of @Transactional annotation on %1$s:" + "%n%2$cvalue=\"%3$s\"",
                createNewAuthorStaticMethod.toString(), '\t', (name == null ? "" : name));
        int isolationLevel = transactionAttribute.getIsolationLevel();
        Assert.assertEquals(TransactionAttribute.ISOLATION_DEFAULT, isolationLevel);
        switch (isolationLevel) {
        case TransactionAttribute.ISOLATION_DEFAULT:
            message = message + String.format("%n%1$cisolation=ISOLATION_DEFAULT", '\t');
            break;
        case TransactionAttribute.ISOLATION_READ_COMMITTED:
            message = message + String.format("%n%1$cisolation=ISOLATION_READ_COMMITTED", '\t');
            break;
        case TransactionAttribute.ISOLATION_READ_UNCOMMITTED:
            message = message + String.format("%n%1$cisolation=ISOLATION_READ_UNCOMMITTED", '\t');
            break;
        case TransactionAttribute.ISOLATION_REPEATABLE_READ:
            message = message + String.format("%n%1$cisolation=ISOLATION_REPEATABLE_READ", '\t');
            break;
        case TransactionAttribute.ISOLATION_SERIALIZABLE:
            message = message + String.format("%n%1$cisolation=ISOLATION_SERIALIZABLE", '\t');
            break;
        default:
            Assert.fail("Unrecognizable isolation level.");
        } // switch

        int propagationBehavior = transactionAttribute.getPropagationBehavior();
        //Default propagation behavior seems to be TransactionAttribute.PROPAGATION_REQUIRED
        Assert.assertEquals(TransactionAttribute.PROPAGATION_REQUIRED, propagationBehavior);
        switch (propagationBehavior) {
        case TransactionAttribute.PROPAGATION_MANDATORY:
            message = message + String.format("%n%1$cpropagation=PROPAGATION_MANDATORY", '\t');
            break;
        case TransactionAttribute.PROPAGATION_NESTED:
            message = message + String.format("%n%1$cpropagation=PROPAGATION_NESTED", '\t');
            break;
        case TransactionAttribute.PROPAGATION_NEVER:
            message = message + String.format("%n%1$cpropagation=PROPAGATION_NEVER", '\t');
            break;
        case TransactionAttribute.PROPAGATION_NOT_SUPPORTED:
            message = message + String.format("%n%1$cpropagation=PROPAGATION_NOT_SUPPORTED", '\t');
            break;
        case TransactionAttribute.PROPAGATION_REQUIRED:
            message = message + String.format("%n%1$cpropagation=PROPAGATION_REQUIRED", '\t');
            break;
        case TransactionAttribute.PROPAGATION_REQUIRES_NEW:
            message = message + String.format("%n%1$cpropagation=PROPAGATION_REQUIRES_NEW", '\t');
            break;
        case TransactionAttribute.PROPAGATION_SUPPORTS:
            message = message + String.format("%n%1$cpropagation=PROPAGATION_SUPPORTS", '\t');
            break;
        default:
            Assert.fail("Unrecognizable propagation behavior.");
        } // switch

        String qualifier = transactionAttribute.getQualifier();
        message = message + String.format("%n%1$cqualifier=%2$s", '\t', qualifier);

        int timeout = transactionAttribute.getTimeout();
        Assert.assertEquals(TransactionAttribute.TIMEOUT_DEFAULT, timeout);
        message = message + String.format("%n%1$ctimeout=%2$d", '\t', timeout);

        boolean readOnlyFlag = transactionAttribute.isReadOnly();
        Assert.assertFalse(readOnlyFlag);
        message = message + String.format("%n%1$creadOnly=%2$s", '\t', String.valueOf(readOnlyFlag));

        if (logger.isDebugEnabled()) {
            logger.debug(message);
        }
    } catch (Throwable throwable) {
        throw throwable;
    }
}

From source file:org.springframework.transaction.interceptor.TransactionAspectSupport.java

/**
 * General delegate for around-advice-based subclasses, delegating to several other template
 * methods on this class. Able to handle {@link CallbackPreferringPlatformTransactionManager}
 * as well as regular {@link PlatformTransactionManager} implementations.
 * @param method the Method being invoked
 * @param targetClass the target class that we're invoking the method on
 * @param invocation the callback to use for proceeding with the target invocation
 * @return the return value of the method, if any
 * @throws Throwable propagated from the target invocation
 *//*  w  w w .j a  va  2  s. c  o m*/
@Nullable
protected Object invokeWithinTransaction(Method method, @Nullable Class<?> targetClass,
        final InvocationCallback invocation) throws Throwable {

    // If the transaction attribute is null, the method is non-transactional.
    TransactionAttributeSource tas = getTransactionAttributeSource();
    final TransactionAttribute txAttr = (tas != null ? tas.getTransactionAttribute(method, targetClass) : null);
    final PlatformTransactionManager tm = determineTransactionManager(txAttr);
    final String joinpointIdentification = methodIdentification(method, targetClass, txAttr);

    if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) {
        // Standard transaction demarcation with getTransaction and commit/rollback calls.
        TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification);
        Object retVal = null;
        try {
            // This is an around advice: Invoke the next interceptor in the chain.
            // This will normally result in a target object being invoked.
            retVal = invocation.proceedWithInvocation();
        } catch (Throwable ex) {
            // target invocation exception
            completeTransactionAfterThrowing(txInfo, ex);
            throw ex;
        } finally {
            cleanupTransactionInfo(txInfo);
        }
        commitTransactionAfterReturning(txInfo);
        return retVal;
    }

    else {
        final ThrowableHolder throwableHolder = new ThrowableHolder();

        // It's a CallbackPreferringPlatformTransactionManager: pass a TransactionCallback in.
        try {
            Object result = ((CallbackPreferringPlatformTransactionManager) tm).execute(txAttr, status -> {
                TransactionInfo txInfo = prepareTransactionInfo(tm, txAttr, joinpointIdentification, status);
                try {
                    return invocation.proceedWithInvocation();
                } catch (Throwable ex) {
                    if (txAttr.rollbackOn(ex)) {
                        // A RuntimeException: will lead to a rollback.
                        if (ex instanceof RuntimeException) {
                            throw (RuntimeException) ex;
                        } else {
                            throw new ThrowableHolderException(ex);
                        }
                    } else {
                        // A normal return value: will lead to a commit.
                        throwableHolder.throwable = ex;
                        return null;
                    }
                } finally {
                    cleanupTransactionInfo(txInfo);
                }
            });

            // Check result state: It might indicate a Throwable to rethrow.
            if (throwableHolder.throwable != null) {
                throw throwableHolder.throwable;
            }
            return result;
        } catch (ThrowableHolderException ex) {
            throw ex.getCause();
        } catch (TransactionSystemException ex2) {
            if (throwableHolder.throwable != null) {
                logger.error("Application exception overridden by commit exception", throwableHolder.throwable);
                ex2.initApplicationException(throwableHolder.throwable);
            }
            throw ex2;
        } catch (Throwable ex2) {
            if (throwableHolder.throwable != null) {
                logger.error("Application exception overridden by commit exception", throwableHolder.throwable);
            }
            throw ex2;
        }
    }
}