Skip to content

Commit a0bbaf0

Browse files
committed
Merge branch '3.1.x' into 3.2.x
2 parents 3433ece + 5efc269 commit a0bbaf0

File tree

3 files changed

+28
-25
lines changed

3 files changed

+28
-25
lines changed

grails-core/src/main/groovy/grails/transaction/TransactionManagerAware.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package grails.transaction;
1717

1818
import org.springframework.beans.factory.Aware;
19+
import org.springframework.beans.factory.annotation.Autowired;
1920
import org.springframework.transaction.PlatformTransactionManager;
2021

2122
/**
@@ -29,5 +30,6 @@ public interface TransactionManagerAware extends Aware {
2930
*
3031
* @param transactionManager The TransactionManager implementation
3132
*/
33+
@Autowired
3234
void setTransactionManager(PlatformTransactionManager transactionManager);
3335
}

grails-core/src/main/groovy/org/grails/transaction/TransactionManagerPostProcessor.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class TransactionManagerPostProcessor extends InstantiationAwareBeanPostP
3838
private ConfigurableListableBeanFactory beanFactory;
3939
private PlatformTransactionManager transactionManager;
4040
private int order = Ordered.LOWEST_PRECEDENCE;
41+
private boolean initialized = false;
4142

4243
/**
4344
* Gets the platform transaction manager from the bean factory if
@@ -50,7 +51,6 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
5051
"TransactionManagerPostProcessor requires a ConfigurableListableBeanFactory");
5152

5253
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
53-
initialize();
5454
}
5555

5656
/**
@@ -64,28 +64,34 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
6464
@Override
6565
public boolean postProcessAfterInstantiation(Object bean, String name) throws BeansException {
6666
if (bean instanceof TransactionManagerAware) {
67-
TransactionManagerAware tma = (TransactionManagerAware) bean;
68-
tma.setTransactionManager(transactionManager);
67+
initialize();
68+
if(transactionManager != null) {
69+
TransactionManagerAware tma = (TransactionManagerAware) bean;
70+
tma.setTransactionManager(transactionManager);
71+
}
6972
}
7073
return true;
7174
}
7275

7376
private void initialize() {
74-
if (beanFactory.containsBean(GrailsApplication.TRANSACTION_MANAGER_BEAN)) {
75-
transactionManager = beanFactory.getBean(GrailsApplication.TRANSACTION_MANAGER_BEAN, PlatformTransactionManager.class);
76-
} else {
77-
// Fetch the names of all the beans that are of type
78-
// PlatformTransactionManager. Note that we have to pass
79-
// "false" for the last argument to avoid eager initialisation,
80-
// otherwise we end up in an endless loop (it triggers the current method).
81-
String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
82-
beanFactory, PlatformTransactionManager.class, false, false);
77+
if(transactionManager == null && beanFactory != null && !initialized) {
78+
if (beanFactory.containsBean(GrailsApplication.TRANSACTION_MANAGER_BEAN)) {
79+
transactionManager = beanFactory.getBean(GrailsApplication.TRANSACTION_MANAGER_BEAN, PlatformTransactionManager.class);
80+
} else {
81+
// Fetch the names of all the beans that are of type
82+
// PlatformTransactionManager. Note that we have to pass
83+
// "false" for the last argument to avoid eager initialisation,
84+
// otherwise we end up in an endless loop (it triggers the current method).
85+
String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
86+
beanFactory, PlatformTransactionManager.class, false, false);
8387

84-
// If at least one is found, use the first of them as the
85-
// transaction manager for the application.
86-
if (beanNames.length > 0) {
87-
transactionManager = (PlatformTransactionManager)beanFactory.getBean(beanNames[0]);
88+
// If at least one is found, use the first of them as the
89+
// transaction manager for the application.
90+
if (beanNames.length > 0) {
91+
transactionManager = (PlatformTransactionManager)beanFactory.getBean(beanNames[0]);
92+
}
8893
}
94+
initialized = true;
8995
}
9096
}
9197

grails-plugin-datasource/src/main/groovy/org/grails/plugins/datasource/DataSourceGrailsPlugin.groovy

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,16 @@
1616
package org.grails.plugins.datasource
1717

1818
import grails.config.Config
19-
import grails.core.support.GrailsApplicationAware
19+
import grails.core.GrailsApplication
2020
import grails.plugins.Plugin
2121
import grails.util.Environment
2222
import grails.util.GrailsUtil
23-
import grails.util.Metadata
24-
import org.springframework.context.ApplicationContext
25-
import org.springframework.context.ApplicationContextAware
26-
27-
import javax.sql.DataSource
28-
2923
import org.apache.commons.logging.Log
3024
import org.apache.commons.logging.LogFactory
3125
import org.apache.tomcat.jdbc.pool.DataSource as TomcatDataSource
32-
import grails.core.GrailsApplication
3326
import org.grails.core.exceptions.GrailsConfigurationException
34-
import org.grails.transaction.TransactionManagerPostProcessor
3527
import org.grails.transaction.ChainedTransactionManagerPostProcessor
28+
import org.grails.transaction.TransactionManagerPostProcessor
3629
import org.springframework.jdbc.datasource.DataSourceTransactionManager
3730
import org.springframework.jdbc.datasource.DriverManagerDataSource
3831
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy
@@ -41,6 +34,8 @@ import org.springframework.jmx.support.JmxUtils
4134
import org.springframework.jndi.JndiObjectFactoryBean
4235
import org.springframework.util.ClassUtils
4336

37+
import javax.sql.DataSource
38+
4439
/**
4540
* Handles the configuration of a DataSource within Grails.
4641
*

0 commit comments

Comments
 (0)