diff --git a/spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionAspectSupport.java b/spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionAspectSupport.java index ebc56af8f109..7d9a237469b9 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionAspectSupport.java +++ b/spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionAspectSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -192,7 +192,7 @@ protected TransactionAspectSupport() { /** * Specify the name of the default transaction manager bean. - * This can either point to a traditional {@link PlatformTransactionManager} or a + *

This can either point to a traditional {@link PlatformTransactionManager} or a * {@link ReactiveTransactionManager} for reactive transaction management. */ public void setTransactionManagerBeanName(@Nullable String transactionManagerBeanName) { @@ -209,7 +209,7 @@ protected final String getTransactionManagerBeanName() { /** * Specify the default transaction manager to use to drive transactions. - * This can either be a traditional {@link PlatformTransactionManager} or a + *

This can either be a traditional {@link PlatformTransactionManager} or a * {@link ReactiveTransactionManager} for reactive transaction management. *

The default transaction manager will be used if a qualifier * has not been declared for a given transaction or if an explicit name for the @@ -222,7 +222,7 @@ public void setTransactionManager(@Nullable TransactionManager transactionManage /** * Return the default transaction manager, or {@code null} if unknown. - * This can either be a traditional {@link PlatformTransactionManager} or a + *

This can either be a traditional {@link PlatformTransactionManager} or a * {@link ReactiveTransactionManager} for reactive transaction management. */ @Nullable @@ -375,7 +375,7 @@ protected Object invokeWithinTransaction(Method method, @Nullable Class targe cleanupTransactionInfo(txInfo); } - if (vavrPresent && VavrDelegate.isVavrTry(retVal)) { + if (retVal != null && vavrPresent && VavrDelegate.isVavrTry(retVal)) { // Set rollback-only in case of Vavr failure matching our rollback rules... TransactionStatus status = txInfo.getTransactionStatus(); if (status != null && txAttr != null) { @@ -388,15 +388,16 @@ protected Object invokeWithinTransaction(Method method, @Nullable Class targe } else { + Object result; final ThrowableHolder throwableHolder = new ThrowableHolder(); // It's a CallbackPreferringPlatformTransactionManager: pass a TransactionCallback in. try { - Object result = ((CallbackPreferringPlatformTransactionManager) ptm).execute(txAttr, status -> { + result = ((CallbackPreferringPlatformTransactionManager) ptm).execute(txAttr, status -> { TransactionInfo txInfo = prepareTransactionInfo(ptm, txAttr, joinpointIdentification, status); try { Object retVal = invocation.proceedWithInvocation(); - if (vavrPresent && VavrDelegate.isVavrTry(retVal)) { + if (retVal != null && vavrPresent && VavrDelegate.isVavrTry(retVal)) { // Set rollback-only in case of Vavr failure matching our rollback rules... retVal = VavrDelegate.evaluateTryFailure(retVal, txAttr, status); } @@ -422,12 +423,6 @@ protected Object invokeWithinTransaction(Method method, @Nullable Class targe 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(); @@ -445,11 +440,17 @@ protected Object invokeWithinTransaction(Method method, @Nullable Class targe } throw ex2; } + + // Check result state: It might indicate a Throwable to rethrow. + if (throwableHolder.throwable != null) { + throw throwableHolder.throwable; + } + return result; } } /** - * Clear the cache. + * Clear the transaction manager cache. */ protected void clearTransactionManagerCache() { this.transactionManagerCache.clear(); @@ -780,6 +781,7 @@ public String toString() { @FunctionalInterface protected interface InvocationCallback { + @Nullable Object proceedWithInvocation() throws Throwable; }