Skip to content

Commit b8106ae

Browse files
committed
Improve transaction manager detection
Switch the condition used to trigger the creation of a transaction manager from the default name to the actual type. Fixes gh-3012
1 parent bd6f7a5 commit b8106ae

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceTransactionManagerAutoConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@
3737
* {@link DataSourceTransactionManager}.
3838
*
3939
* @author Dave Syer
40+
* @author Stephane Nicoll
4041
*/
4142
@Configuration
4243
@ConditionalOnClass({ JdbcTemplate.class, PlatformTransactionManager.class })
@@ -51,7 +52,7 @@ public int getOrder() {
5152
private DataSource dataSource;
5253

5354
@Bean
54-
@ConditionalOnMissingBean(name = "transactionManager")
55+
@ConditionalOnMissingBean
5556
@ConditionalOnBean(DataSource.class)
5657
public PlatformTransactionManager transactionManager() {
5758
return new DataSourceTransactionManager(this.dataSource);

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceTransactionManagerAutoConfigurationTests.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,17 +20,22 @@
2020

2121
import org.junit.Test;
2222
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
2325
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
26+
import org.springframework.transaction.PlatformTransactionManager;
2427
import org.springframework.transaction.annotation.AbstractTransactionManagementConfiguration;
2528
import org.springframework.transaction.annotation.EnableTransactionManagement;
2629

2730
import static org.junit.Assert.assertEquals;
2831
import static org.junit.Assert.assertNotNull;
32+
import static org.mockito.Mockito.mock;
2933

3034
/**
3135
* Tests for {@link DataSourceTransactionManagerAutoConfiguration}.
3236
*
3337
* @author Dave Syer
38+
* @author Stephane Nicoll
3439
*/
3540
public class DataSourceTransactionManagerAutoConfigurationTests {
3641

@@ -67,9 +72,32 @@ public void testManualConfiguration() throws Exception {
6772
assertNotNull(this.context.getBean(DataSourceTransactionManager.class));
6873
}
6974

75+
@Test
76+
public void testExistingTransactionManager() {
77+
this.context.register(SwitchTransactionsOn.class,
78+
TransactionManagerConfiguration.class,
79+
EmbeddedDataSourceConfiguration.class,
80+
DataSourceTransactionManagerAutoConfiguration.class);
81+
this.context.refresh();
82+
assertEquals("No transaction manager should be been created", 1,
83+
this.context.getBeansOfType(PlatformTransactionManager.class).size());
84+
assertEquals("Wrong transaction manager", this.context.getBean("myTransactionManager"),
85+
this.context.getBean(PlatformTransactionManager.class));
86+
}
87+
7088
@EnableTransactionManagement
7189
protected static class SwitchTransactionsOn {
7290

7391
}
7492

93+
@Configuration
94+
protected static class TransactionManagerConfiguration {
95+
96+
@Bean
97+
public PlatformTransactionManager myTransactionManager() {
98+
return mock(PlatformTransactionManager.class);
99+
}
100+
101+
}
102+
75103
}

0 commit comments

Comments
 (0)