TransactionAspectSupport stores given PlatformTransactionManager instance as strong reference

Issue: SPR-14609
This commit is contained in:
Juergen Hoeller 2016-08-24 14:31:02 +02:00
parent 3d297b10e9
commit 951ac5ea4f
1 changed files with 13 additions and 9 deletions

View File

@ -128,6 +128,8 @@ public abstract class TransactionAspectSupport implements BeanFactoryAware, Init
private String transactionManagerBeanName; private String transactionManagerBeanName;
private PlatformTransactionManager transactionManager;
private TransactionAttributeSource transactionAttributeSource; private TransactionAttributeSource transactionAttributeSource;
private BeanFactory beanFactory; private BeanFactory beanFactory;
@ -158,16 +160,14 @@ public abstract class TransactionAspectSupport implements BeanFactoryAware, Init
* @see #setTransactionManagerBeanName * @see #setTransactionManagerBeanName
*/ */
public void setTransactionManager(PlatformTransactionManager transactionManager) { public void setTransactionManager(PlatformTransactionManager transactionManager) {
if (transactionManager != null) { this.transactionManager = transactionManager;
this.transactionManagerCache.put(DEFAULT_TRANSACTION_MANAGER_KEY, transactionManager);
}
} }
/** /**
* Return the default transaction manager, or {@code null} if unknown. * Return the default transaction manager, or {@code null} if unknown.
*/ */
public PlatformTransactionManager getTransactionManager() { 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 @Override
public void afterPropertiesSet() { public void afterPropertiesSet() {
if (getTransactionManager() == null && this.beanFactory == null) { if (getTransactionManager() == null && getBeanFactory() == null) {
throw new IllegalStateException( throw new IllegalStateException(
"Setting the property 'transactionManager' or running in a BeanFactory is required"); "Setting the property 'transactionManager' or running in a BeanFactory is required");
} }
if (this.transactionAttributeSource == null) { if (getTransactionAttributeSource() == null) {
throw new IllegalStateException( throw new IllegalStateException(
"Either 'transactionAttributeSource' or 'transactionAttributes' is required: " + "Either 'transactionAttributeSource' or 'transactionAttributes' is required: " +
"If there are no transactional methods, then don't use a transaction aspect."); "If there are no transactional methods, then don't use a transaction aspect.");
@ -362,11 +362,14 @@ public abstract class TransactionAspectSupport implements BeanFactoryAware, Init
} }
else { else {
PlatformTransactionManager defaultTransactionManager = getTransactionManager(); PlatformTransactionManager defaultTransactionManager = getTransactionManager();
if (defaultTransactionManager == null) {
defaultTransactionManager = this.transactionManagerCache.get(DEFAULT_TRANSACTION_MANAGER_KEY);
if (defaultTransactionManager == null) { if (defaultTransactionManager == null) {
defaultTransactionManager = this.beanFactory.getBean(PlatformTransactionManager.class); defaultTransactionManager = this.beanFactory.getBean(PlatformTransactionManager.class);
this.transactionManagerCache.putIfAbsent( this.transactionManagerCache.putIfAbsent(
DEFAULT_TRANSACTION_MANAGER_KEY, defaultTransactionManager); DEFAULT_TRANSACTION_MANAGER_KEY, defaultTransactionManager);
} }
}
return defaultTransactionManager; return defaultTransactionManager;
} }
} }
@ -567,6 +570,7 @@ public abstract class TransactionAspectSupport implements BeanFactoryAware, Init
public TransactionInfo(PlatformTransactionManager transactionManager, public TransactionInfo(PlatformTransactionManager transactionManager,
TransactionAttribute transactionAttribute, String joinpointIdentification) { TransactionAttribute transactionAttribute, String joinpointIdentification) {
this.transactionManager = transactionManager; this.transactionManager = transactionManager;
this.transactionAttribute = transactionAttribute; this.transactionAttribute = transactionAttribute;
this.joinpointIdentification = joinpointIdentification; this.joinpointIdentification = joinpointIdentification;