Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable transaction management consistently
Previously to this commit, transaction management was only enabled when a `DataSource` is configured. The processing of `@Transactional` annotations are now enabled as long as a `PlatformTransactionManager` is present. Also, the `spring.aop.proxy-target-class` is now honoured if set, still defaulting to CGLIB mode. Closes gh-8434
- Loading branch information
Showing
8 changed files
with
213 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...ramework/boot/autoconfigure/transaction/EnableTransactionManagementAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2012-2017 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.autoconfigure.transaction; | ||
|
||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.transaction.annotation.AbstractTransactionManagementConfiguration; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
|
||
/** | ||
* Enables Spring's annotation-driven transaction management capability. | ||
* | ||
* @author Stephane Nicoll | ||
* @since 1.4.6 | ||
*/ | ||
@Configuration | ||
@ConditionalOnClass(EnableTransactionManagement.class) | ||
@ConditionalOnMissingBean(AbstractTransactionManagementConfiguration.class) | ||
@ConditionalOnBean(PlatformTransactionManager.class) | ||
@AutoConfigureAfter(TransactionAutoConfiguration.class) | ||
class EnableTransactionManagementAutoConfiguration { | ||
|
||
@Configuration | ||
@EnableTransactionManagement(proxyTargetClass = false) | ||
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false", matchIfMissing = false) | ||
public static class JdkDynamicAutoProxyConfiguration { | ||
|
||
} | ||
|
||
@Configuration | ||
@EnableTransactionManagement(proxyTargetClass = true) | ||
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = true) | ||
public static class CglibAutoProxyConfiguration { | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
...ork/boot/autoconfigure/transaction/EnableTransactionManagementAutoConfigurationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* Copyright 2012-2017 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.autoconfigure.transaction; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import org.junit.After; | ||
import org.junit.Test; | ||
|
||
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; | ||
import org.springframework.boot.test.util.EnvironmentTestUtils; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.jdbc.datasource.DataSourceTransactionManager; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link EnableTransactionManagementAutoConfiguration}. | ||
* | ||
* @author Stephane Nicoll | ||
*/ | ||
public class EnableTransactionManagementAutoConfigurationTests { | ||
|
||
private AnnotationConfigApplicationContext context; | ||
|
||
@After | ||
public void tearDown() { | ||
if (this.context != null) { | ||
this.context.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void transactionNotManagedWithNoTransactionManager() { | ||
load(BaseConfiguration.class); | ||
assertThat(this.context.getBean(TransactionalService.class) | ||
.isTransactionActive()).isFalse(); | ||
} | ||
|
||
@Test | ||
public void transactionManagerUsesCglibByDefault() { | ||
load(TransactionManagersConfiguration.class); | ||
assertThat(this.context.getBean(AnotherServiceImpl.class) | ||
.isTransactionActive()).isTrue(); | ||
assertThat(this.context.getBeansOfType(TransactionalServiceImpl.class)).hasSize(1); | ||
} | ||
|
||
@Test | ||
public void transactionManagerCanBeConfiguredToJdkProxy() { | ||
load(TransactionManagersConfiguration.class, "spring.aop.proxy-target-class=false"); | ||
assertThat(this.context.getBean(AnotherService.class) | ||
.isTransactionActive()).isTrue(); | ||
assertThat(this.context.getBeansOfType(AnotherServiceImpl.class)).hasSize(0); | ||
assertThat(this.context.getBeansOfType(TransactionalServiceImpl.class)).hasSize(0); | ||
} | ||
|
||
private void load(Class<?> config, String... environment) { | ||
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); | ||
applicationContext.register(config); | ||
applicationContext.register(EnableTransactionManagementAutoConfiguration.class); | ||
EnvironmentTestUtils.addEnvironment(applicationContext, environment); | ||
applicationContext.refresh(); | ||
this.context = applicationContext; | ||
} | ||
|
||
@Configuration | ||
static class BaseConfiguration { | ||
|
||
@Bean | ||
public TransactionalService transactionalService() { | ||
return new TransactionalServiceImpl(); | ||
} | ||
|
||
@Bean | ||
public AnotherServiceImpl anotherService() { | ||
return new AnotherServiceImpl(); | ||
} | ||
} | ||
|
||
@Configuration | ||
@Import(BaseConfiguration.class) | ||
static class TransactionManagersConfiguration { | ||
|
||
@Bean | ||
public DataSourceTransactionManager transactionManager() { | ||
return new DataSourceTransactionManager(dataSource()); | ||
} | ||
|
||
@Bean | ||
public DataSource dataSource() { | ||
return DataSourceBuilder.create() | ||
.driverClassName("org.hsqldb.jdbc.JDBCDriver") | ||
.url("jdbc:hsqldb:mem:tx").username("sa").build(); | ||
} | ||
|
||
} | ||
|
||
interface TransactionalService { | ||
|
||
@Transactional | ||
boolean isTransactionActive(); | ||
|
||
} | ||
|
||
static class TransactionalServiceImpl implements TransactionalService { | ||
|
||
|
||
@Override | ||
public boolean isTransactionActive() { | ||
return TransactionSynchronizationManager.isActualTransactionActive(); | ||
} | ||
} | ||
|
||
interface AnotherService { | ||
|
||
boolean isTransactionActive(); | ||
|
||
} | ||
|
||
static class AnotherServiceImpl implements AnotherService { | ||
|
||
|
||
@Override | ||
@Transactional | ||
public boolean isTransactionActive() { | ||
return TransactionSynchronizationManager.isActualTransactionActive(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters