From 961574bd17b2fe30f171646f54667b5895f0dcbf Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Sun, 28 Dec 2014 18:17:01 +0100 Subject: [PATCH] Fix regression in determineTransactionManager One more (and hopefully last) attempt at making sure determineTransactionManager does not break existing use cases. This commit prevents any lookup if no transaction attributes are set which is more compliant with the original version and prevents a lookup if a non existing bean name is provided explicitly (as it can be the case with Spring Boot). Issue: SPR-12541 --- .../interceptor/TransactionAspectSupport.java | 47 +++++++++---------- 1 file changed, 21 insertions(+), 26 deletions(-) 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 48c8f7c47d..567ffd7f1b 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 @@ -349,35 +349,30 @@ public abstract class TransactionAspectSupport implements BeanFactoryAware, Init * Determine the specific transaction manager to use for the given transaction. */ protected PlatformTransactionManager determineTransactionManager(TransactionAttribute txAttr) { - if (this.beanFactory != null) { - String qualifier = txAttr != null ? txAttr.getQualifier() : null; - if (StringUtils.hasText(qualifier)) { - return determineQualifiedTransactionManager(qualifier); - } - else if (StringUtils.hasText(this.transactionManagerBeanName)) { - return determineQualifiedTransactionManager(this.transactionManagerBeanName); - } - else if (txAttr != null) { // Do not lookup default bean name if no tx attributes are set - PlatformTransactionManager defaultTransactionManager = getTransactionManager(); - if (defaultTransactionManager == null) { - defaultTransactionManager = this.beanFactory.getBean(PlatformTransactionManager.class); - this.transactionManagerCache.putIfAbsent( - DEFAULT_TRANSACTION_MANAGER_KEY, defaultTransactionManager); - } - return defaultTransactionManager; + // Do not attempt to lookup tx manager if no tx attributes are set + if (txAttr == null || this.beanFactory == null) { + return getTransactionManager(); + } + String qualifier = (txAttr.getQualifier() != null ? + txAttr.getQualifier() : this.transactionManagerBeanName); + if (StringUtils.hasText(qualifier)) { + PlatformTransactionManager txManager = this.transactionManagerCache.get(qualifier); + if (txManager == null) { + txManager = BeanFactoryAnnotationUtils.qualifiedBeanOfType( + this.beanFactory, PlatformTransactionManager.class, qualifier); + this.transactionManagerCache.putIfAbsent(qualifier, txManager); } + return txManager; } - return getTransactionManager(); - } - - private PlatformTransactionManager determineQualifiedTransactionManager(String qualifier) { - PlatformTransactionManager txManager = this.transactionManagerCache.get(qualifier); - if (txManager == null) { - txManager = BeanFactoryAnnotationUtils.qualifiedBeanOfType( - this.beanFactory, PlatformTransactionManager.class, qualifier); - this.transactionManagerCache.putIfAbsent(qualifier, txManager); + else { + PlatformTransactionManager defaultTransactionManager = getTransactionManager(); + if (defaultTransactionManager == null) { + defaultTransactionManager = this.beanFactory.getBean(PlatformTransactionManager.class); + this.transactionManagerCache.putIfAbsent( + DEFAULT_TRANSACTION_MANAGER_KEY, defaultTransactionManager); + } + return defaultTransactionManager; } - return txManager; } /**