67
67
*/
68
68
public abstract class TransactionAspectSupport implements BeanFactoryAware , InitializingBean {
69
69
70
+ /**
71
+ * Key to use to store the default transaction manager.
72
+ */
73
+ private final Object DEFAULT_TRANSACTION_MANAGER_KEY = new Object ();
74
+
70
75
// NOTE: This class must not implement Serializable because it serves as base
71
76
// class for AspectJ aspects (which are not allowed to implement Serializable)!
72
77
@@ -80,8 +85,8 @@ public abstract class TransactionAspectSupport implements BeanFactoryAware, Init
80
85
new NamedThreadLocal <TransactionInfo >("Current aspect-driven transaction" );
81
86
82
87
83
- private final ConcurrentHashMap <String , PlatformTransactionManager > transactionManagerCache =
84
- new ConcurrentHashMap <String , PlatformTransactionManager >();
88
+ private final ConcurrentHashMap <Object , PlatformTransactionManager > transactionManagerCache =
89
+ new ConcurrentHashMap <Object , PlatformTransactionManager >();
85
90
86
91
/**
87
92
* Subclasses can use this to return the current TransactionInfo.
@@ -127,11 +132,6 @@ public static TransactionStatus currentTransactionStatus() throws NoTransactionE
127
132
*/
128
133
private String transactionManagerBeanName ;
129
134
130
- /**
131
- * Default transaction manager.
132
- */
133
- private PlatformTransactionManager transactionManager ;
134
-
135
135
private TransactionAttributeSource transactionAttributeSource ;
136
136
137
137
private BeanFactory beanFactory ;
@@ -159,14 +159,16 @@ protected final String getTransactionManagerBeanName() {
159
159
* @see #setTransactionManagerBeanName
160
160
*/
161
161
public void setTransactionManager (PlatformTransactionManager transactionManager ) {
162
- this .transactionManager = transactionManager ;
162
+ if (transactionManager != null ) {
163
+ this .transactionManagerCache .put (DEFAULT_TRANSACTION_MANAGER_KEY , transactionManager );
164
+ }
163
165
}
164
166
165
167
/**
166
168
* Return the default transaction manager, or {@code null} if unknown.
167
169
*/
168
170
public PlatformTransactionManager getTransactionManager () {
169
- return this .transactionManager ;
171
+ return this .transactionManagerCache . get ( DEFAULT_TRANSACTION_MANAGER_KEY ) ;
170
172
}
171
173
172
174
/**
@@ -239,7 +241,7 @@ protected final BeanFactory getBeanFactory() {
239
241
*/
240
242
@ Override
241
243
public void afterPropertiesSet () {
242
- if (this . transactionManager == null && this .beanFactory == null ) {
244
+ if (getTransactionManager () == null && this .beanFactory == null ) {
243
245
throw new IllegalStateException (
244
246
"Setting the property 'transactionManager' or running in a ListableBeanFactory is required" );
245
247
}
@@ -340,33 +342,36 @@ public Object doInTransaction(TransactionStatus status) {
340
342
* Determine the specific transaction manager to use for the given transaction.
341
343
*/
342
344
protected PlatformTransactionManager determineTransactionManager (TransactionAttribute txAttr ) {
343
- if (this .transactionManager != null || this . beanFactory == null || txAttr = = null ) {
344
- return this . transactionManager ;
345
- }
346
- String qualifier = txAttr . getQualifier ( );
347
- if (StringUtils . hasText ( qualifier ) ) {
348
- PlatformTransactionManager txManager = this . transactionManagerCache . get ( qualifier );
349
- if ( txManager == null ) {
350
- txManager = BeanFactoryAnnotationUtils . qualifiedBeanOfType (
351
- this . beanFactory , PlatformTransactionManager . class , qualifier );
352
- this . transactionManagerCache . putIfAbsent ( qualifier , txManager ) ;
345
+ if (this .beanFactory ! = null ) {
346
+ String qualifier = txAttr != null ? txAttr . getQualifier () : null ;
347
+ if ( StringUtils . hasText ( qualifier )) {
348
+ PlatformTransactionManager txManager = this . transactionManagerCache . get ( qualifier );
349
+ if (txManager == null ) {
350
+ txManager = BeanFactoryAnnotationUtils . qualifiedBeanOfType (
351
+ this . beanFactory , PlatformTransactionManager . class , qualifier );
352
+ this . transactionManagerCache . putIfAbsent ( qualifier , txManager );
353
+ }
354
+ return txManager ;
353
355
}
354
- return txManager ;
355
- }
356
- else if (StringUtils .hasText (this .transactionManagerBeanName )) {
357
- PlatformTransactionManager txManager = this .transactionManagerCache .get (this .transactionManagerBeanName );
358
- if (txManager == null ) {
359
- txManager = this .beanFactory .getBean (
360
- this .transactionManagerBeanName , PlatformTransactionManager .class );
361
- this .transactionManagerCache .putIfAbsent (this .transactionManagerBeanName , txManager );
356
+ else if (StringUtils .hasText (this .transactionManagerBeanName )) {
357
+ PlatformTransactionManager txManager = this .transactionManagerCache .get (this .transactionManagerBeanName );
358
+ if (txManager == null ) {
359
+ txManager = this .beanFactory .getBean (
360
+ this .transactionManagerBeanName , PlatformTransactionManager .class );
361
+ this .transactionManagerCache .putIfAbsent (this .transactionManagerBeanName , txManager );
362
+ }
363
+ return txManager ;
364
+ } else {
365
+ PlatformTransactionManager defaultTransactionManager = getTransactionManager ();
366
+ if (defaultTransactionManager == null ) {
367
+ defaultTransactionManager = this .beanFactory .getBean (PlatformTransactionManager .class );
368
+ this .transactionManagerCache .putIfAbsent (
369
+ DEFAULT_TRANSACTION_MANAGER_KEY , defaultTransactionManager );
370
+ }
371
+ return defaultTransactionManager ;
362
372
}
363
- return txManager ;
364
- }
365
- else {
366
- // Look up the default transaction manager and cache it for subsequent calls
367
- this .transactionManager = this .beanFactory .getBean (PlatformTransactionManager .class );
368
- return this .transactionManager ;
369
373
}
374
+ return getTransactionManager ();
370
375
}
371
376
372
377
/**
0 commit comments