From 9dec27e7bf921a0d5ad3b1d64486a4bff92c1df0 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 4 Nov 2014 14:43:49 +0100 Subject: [PATCH] Permit to disable JTA using a single property Disable JTA auto-configuration altogether with a simple property. This can be useful if the environment is JTA capable but the application does not require it. Fixes gh-1457 --- .../autoconfigure/jta/JtaAutoConfiguration.java | 2 ++ .../additional-spring-configuration-metadata.json | 6 ++++++ .../jta/JtaAutoConfigurationTests.java | 13 +++++++++++++ .../src/main/asciidoc/spring-boot-features.adoc | 4 +++- 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jta/JtaAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jta/JtaAutoConfiguration.java index 4a0742d1bc8..ec41ba8626e 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jta/JtaAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jta/JtaAutoConfiguration.java @@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.jta; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Import; @@ -29,6 +30,7 @@ import org.springframework.context.annotation.Import; * @since 1.2.0 */ @ConditionalOnClass(javax.transaction.Transaction.class) +@ConditionalOnProperty(prefix = "spring.jta", value = "enabled", matchIfMissing = true) @Import({ JndiJtaConfiguration.class, BitronixJtaConfiguration.class, AtomikosJtaConfiguration.class }) @EnableConfigurationProperties(JtaProperties.class) diff --git a/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index b7fe7c6f6a7..4ef041becea 100644 --- a/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -59,6 +59,12 @@ "description": "Automatically register OpenEntityManagerInViewInterceptor. Binds a JPA EntityManager to the thread for the entire processing of the request.", "defaultValue": true }, + { + "name": "spring.jta.enabled", + "dataType": "java.lang.Boolean", + "description": "Enable JTA support.", + "defaultValue": true + }, { "name": "spring.mobile.devicedelegatingviewresolver.enabled", "dataType": "java.lang.Boolean", diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jta/JtaAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jta/JtaAutoConfigurationTests.java index 58395063d45..c7bf3f71585 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jta/JtaAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jta/JtaAutoConfigurationTests.java @@ -49,6 +49,7 @@ import com.atomikos.icatch.jta.UserTransactionManager; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; @@ -87,6 +88,18 @@ public class JtaAutoConfigurationTests { this.context.getBean(JtaTransactionManager.class); } + @Test + public void disableJtaSupport() { + this.context = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.jta.enabled:false"); + this.context.register(JtaAutoConfiguration.class); + this.context.refresh(); + assertEquals(0, this.context.getBeansOfType(JtaTransactionManager.class).size()); + assertEquals(0, this.context.getBeansOfType(XADataSourceWrapper.class).size()); + assertEquals(0, this.context.getBeansOfType(XAConnectionFactoryWrapper.class).size()); + } + @Test public void atomikosSanityCheck() throws Exception { this.context = new AnnotationConfigApplicationContext(JtaProperties.class, diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 7464e86b3a8..ca5eb4af856 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -2032,7 +2032,9 @@ transactions are also supported when deploying to a suitable Java EE Application When a JTA environment is detected, Spring's `JtaTransactionManager` will be used to manage transactions. Auto-configured JMS, DataSource and JPA beans will be upgraded to support XA transactions. You can use standard Spring idioms such as `@Transactional` to -participate in a distributed transaction. +participate in a distributed transaction. If you are within a JTA environment and still +want to use local transactions you can set the `spring.jta.enabled` property to `false` to +disable the JTA auto-configuration.