From 951ac5ea4f36d2c7b0271e75019d1856d1081c82 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 24 Aug 2016 14:31:02 +0200 Subject: [PATCH] TransactionAspectSupport stores given PlatformTransactionManager instance as strong reference Issue: SPR-14609 --- .../interceptor/TransactionAspectSupport.java | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 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 7cc97d1fa6b..7d0e9dd4098 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 @@ -128,6 +128,8 @@ public abstract class TransactionAspectSupport implements BeanFactoryAware, Init private String transactionManagerBeanName; + private PlatformTransactionManager transactionManager; + private TransactionAttributeSource transactionAttributeSource; private BeanFactory beanFactory; @@ -158,16 +160,14 @@ public abstract class TransactionAspectSupport implements BeanFactoryAware, Init * @see #setTransactionManagerBeanName */ public void setTransactionManager(PlatformTransactionManager transactionManager) { - if (transactionManager != null) { - this.transactionManagerCache.put(DEFAULT_TRANSACTION_MANAGER_KEY, transactionManager); - } + this.transactionManager = transactionManager; } /** * Return the default transaction manager, or {@code null} if unknown. */ public PlatformTransactionManager getTransactionManager() { - return this.transactionManagerCache.get(DEFAULT_TRANSACTION_MANAGER_KEY); + return this.transactionManager; } /** @@ -240,11 +240,11 @@ public abstract class TransactionAspectSupport implements BeanFactoryAware, Init */ @Override public void afterPropertiesSet() { - if (getTransactionManager() == null && this.beanFactory == null) { + if (getTransactionManager() == null && getBeanFactory() == null) { throw new IllegalStateException( "Setting the property 'transactionManager' or running in a BeanFactory is required"); } - if (this.transactionAttributeSource == null) { + if (getTransactionAttributeSource() == null) { throw new IllegalStateException( "Either 'transactionAttributeSource' or 'transactionAttributes' is required: " + "If there are no transactional methods, then don't use a transaction aspect."); @@ -363,9 +363,12 @@ public abstract class TransactionAspectSupport implements BeanFactoryAware, Init else { PlatformTransactionManager defaultTransactionManager = getTransactionManager(); if (defaultTransactionManager == null) { - defaultTransactionManager = this.beanFactory.getBean(PlatformTransactionManager.class); - this.transactionManagerCache.putIfAbsent( - DEFAULT_TRANSACTION_MANAGER_KEY, defaultTransactionManager); + defaultTransactionManager = this.transactionManagerCache.get(DEFAULT_TRANSACTION_MANAGER_KEY); + if (defaultTransactionManager == null) { + defaultTransactionManager = this.beanFactory.getBean(PlatformTransactionManager.class); + this.transactionManagerCache.putIfAbsent( + DEFAULT_TRANSACTION_MANAGER_KEY, defaultTransactionManager); + } } return defaultTransactionManager; } @@ -567,6 +570,7 @@ public abstract class TransactionAspectSupport implements BeanFactoryAware, Init public TransactionInfo(PlatformTransactionManager transactionManager, TransactionAttribute transactionAttribute, String joinpointIdentification) { + this.transactionManager = transactionManager; this.transactionAttribute = transactionAttribute; this.joinpointIdentification = joinpointIdentification;