Skip to content

Commit

Permalink
Enable transaction management consistently
Browse files Browse the repository at this point in the history
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
snicoll committed Mar 17, 2017
1 parent 1624370 commit b20dff1
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 34 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* 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.
Expand Down Expand Up @@ -29,8 +29,6 @@
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.AbstractTransactionManagementConfiguration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
* {@link EnableAutoConfiguration Auto-configuration} for
Expand Down Expand Up @@ -63,11 +61,4 @@ public DataSourceTransactionManager transactionManager() {

}

@ConditionalOnMissingBean(AbstractTransactionManagementConfiguration.class)
@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
protected static class TransactionManagementConfiguration {

}

}
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* 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.
Expand Down Expand Up @@ -47,7 +47,6 @@
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.jta.JtaTransactionManager;
import org.springframework.util.ClassUtils;

Expand All @@ -60,8 +59,7 @@
* @author Andy Wilkinson
*/
@Configuration
@ConditionalOnClass({ LocalContainerEntityManagerFactoryBean.class,
EnableTransactionManagement.class, EntityManager.class })
@ConditionalOnClass({ LocalContainerEntityManagerFactoryBean.class, EntityManager.class })
@Conditional(HibernateEntityManagerCondition.class)
@AutoConfigureAfter({ DataSourceAutoConfiguration.class })
public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration {
Expand Down
@@ -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 {

}

}
Expand Up @@ -87,6 +87,7 @@ org.springframework.boot.autoconfigure.social.TwitterAutoConfiguration,\
org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\
org.springframework.boot.autoconfigure.velocity.VelocityAutoConfiguration,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.EnableTransactionManagementAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\
org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration,\
Expand Down
Expand Up @@ -25,8 +25,6 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.AbstractTransactionManagementConfiguration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
Expand All @@ -49,8 +47,6 @@ public void testDataSourceExists() throws Exception {
this.context.refresh();
assertThat(this.context.getBean(DataSource.class)).isNotNull();
assertThat(this.context.getBean(DataSourceTransactionManager.class)).isNotNull();
assertThat(this.context.getBean(AbstractTransactionManagementConfiguration.class))
.isNotNull();
}

@Test
Expand All @@ -64,8 +60,7 @@ public void testNoDataSourceExists() throws Exception {

@Test
public void testManualConfiguration() throws Exception {
this.context.register(SwitchTransactionsOn.class,
EmbeddedDataSourceConfiguration.class,
this.context.register(EmbeddedDataSourceConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(DataSource.class)).isNotNull();
Expand All @@ -74,8 +69,7 @@ public void testManualConfiguration() throws Exception {

@Test
public void testExistingTransactionManager() {
this.context.register(SwitchTransactionsOn.class,
TransactionManagerConfiguration.class,
this.context.register(TransactionManagerConfiguration.class,
EmbeddedDataSourceConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class);
this.context.refresh();
Expand All @@ -92,8 +86,6 @@ public void testMultiDataSource() throws Exception {
this.context.refresh();
assertThat(this.context.getBeansOfType(PlatformTransactionManager.class))
.isEmpty();
assertThat(this.context.getBean(AbstractTransactionManagementConfiguration.class))
.isNotNull();
}

@Test
Expand All @@ -102,13 +94,6 @@ public void testMultiDataSourceUsingPrimary() throws Exception {
DataSourceTransactionManagerAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(DataSourceTransactionManager.class)).isNotNull();
assertThat(this.context.getBean(AbstractTransactionManagementConfiguration.class))
.isNotNull();
}

@EnableTransactionManagement
protected static class SwitchTransactionsOn {

}

@Configuration
Expand Down
@@ -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();
}
}

}
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* 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.
Expand Down Expand Up @@ -32,7 +32,6 @@
import org.springframework.data.gemfire.RegionAttributesFactoryBean;
import org.springframework.data.gemfire.ReplicatedRegionFactoryBean;
import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
* The GemstoneAppConfiguration class for allowing Spring Boot to pick up additional
Expand All @@ -43,7 +42,6 @@
*/
@SpringBootApplication
@EnableGemfireRepositories
@EnableTransactionManagement
@EnableConfigurationProperties(SampleDataGemFireProperties.class)
public class SampleDataGemFireApplication {

Expand Down
Expand Up @@ -11,6 +11,7 @@ org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConf
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.EnableTransactionManagementAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration

# AutoConfigureJson auto-configuration imports
Expand Down

0 comments on commit b20dff1

Please sign in to comment.