From 4a0ac97550c67c926e014903338f9c0e84fa9eee Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Sun, 28 Dec 2014 17:38:06 +0100 Subject: [PATCH] Fix regression in determineTransactionManager The fix in cec26e9 for SPR-12541 actually introduced a regression when the interceptor is enabled on a method that does not require any transaction. In such a case we try to locate the default PlatformTransactionManager instead of just returning what we have (that is null). This commit updates the determineTransactionManager condition again to take that use case into account again. Issue: SPR-12541 --- .../interceptor/TransactionAspectSupport.java | 29 +++++++++---------- .../TransactionInterceptorTests.java | 8 +++++ 2 files changed, 22 insertions(+), 15 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 00679e93876a..48c8f7c47d4f 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 @@ -352,23 +352,12 @@ protected PlatformTransactionManager determineTransactionManager(TransactionAttr if (this.beanFactory != null) { String qualifier = txAttr != null ? txAttr.getQualifier() : null; 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 determineQualifiedTransactionManager(qualifier); } else if (StringUtils.hasText(this.transactionManagerBeanName)) { - PlatformTransactionManager txManager = this.transactionManagerCache.get(this.transactionManagerBeanName); - if (txManager == null) { - txManager = this.beanFactory.getBean( - this.transactionManagerBeanName, PlatformTransactionManager.class); - this.transactionManagerCache.putIfAbsent(this.transactionManagerBeanName, txManager); - } - return txManager; - } else { + 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); @@ -381,6 +370,16 @@ else if (StringUtils.hasText(this.transactionManagerBeanName)) { 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); + } + return txManager; + } + /** * Convenience method to return a String representation of this Method * for use in logging. Can be overridden in subclasses to provide a diff --git a/spring-tx/src/test/java/org/springframework/transaction/interceptor/TransactionInterceptorTests.java b/spring-tx/src/test/java/org/springframework/transaction/interceptor/TransactionInterceptorTests.java index 1613357d5dc5..8fb1a6948fa4 100644 --- a/spring-tx/src/test/java/org/springframework/transaction/interceptor/TransactionInterceptorTests.java +++ b/spring-tx/src/test/java/org/springframework/transaction/interceptor/TransactionInterceptorTests.java @@ -139,6 +139,14 @@ public void determineTransactionManagerWithNoBeanFactoryAndNoTransactionAttribut assertSame(transactionManager, ti.determineTransactionManager(null)); } + @Test + public void determineTransactionManagerWithNoTransactionAttribute() { + BeanFactory beanFactory = mock(BeanFactory.class); + TransactionInterceptor ti = createTestTransactionInterceptor(beanFactory, null); + + assertNull(ti.determineTransactionManager(null)); + } + @Test public void determineTransactionManagerWithQualifierUnknown() { BeanFactory beanFactory = mock(BeanFactory.class);