DATAJPA-685 - Added configuration option to disable default transactions.

@EnableJpaRepositories now has a enableDefaultTransactions attribute that defaults to true. The XML namespace has an equivalent enable-default-transactions attribute on the <jpa:repositories /> element.

Related tickets: DATACMNS-656.
This commit is contained in:
Oliver Gierke
2015-03-10 19:23:14 +01:00
parent 8961b38e05
commit febd41b10b
8 changed files with 269 additions and 3 deletions

View File

@@ -0,0 +1,83 @@
/*
* Copyright 2015 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.data.jpa.repository.support;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import javax.persistence.TransactionRequiredException;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.sample.User;
import org.springframework.data.jpa.repository.sample.UserRepository;
import org.springframework.data.jpa.repository.support.TransactionalRepositoryTests.DelegatingTransactionManager;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Integration tests for disabling default transactions using JavaConfig.
*
* @author Oliver Gierke
* @soundtrack The Intersphere - Live in Mannheim
*/
@RunWith(SpringJUnit4ClassRunner.class)
public abstract class DefaultTransactionDisablingIntegrationTests {
public @Rule ExpectedException exception = ExpectedException.none();
@Autowired UserRepository repository;
@Autowired DelegatingTransactionManager txManager;
/**
* @see DATAJPA-685
*/
@Test
public void considersExplicitConfigurationOnRepositoryInterface() {
repository.findOne(1);
assertThat(txManager.getDefinition().isReadOnly(), is(false));
}
/**
* @see DATAJPA-685
*/
@Test
public void doesNotUseDefaultTransactionsOnNonRedeclaredMethod() {
repository.findAll(new PageRequest(0, 10));
assertThat(txManager.getDefinition(), is(nullValue()));
}
/**
* @see DATAJPA-685
*/
@Test
public void persistingAnEntityShouldThrowExceptionDueToMissingTransaction() {
exception.expect(InvalidDataAccessApiUsageException.class);
exception.expectCause(is(Matchers.<Throwable> instanceOf(TransactionRequiredException.class)));
repository.saveAndFlush(new User());
}
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2015 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.data.jpa.repository.support;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.jpa.repository.sample.UserRepository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* Integration tests for disabled default configurations via JavaConfig.
*
* @author Oliver Gierke
* @soundtrack The Intersphere - Live in Mannheim
*/
@ContextConfiguration
public class JavaConfigDefaultTransactionDisablingIntegrationTests extends DefaultTransactionDisablingIntegrationTests {
@Configuration
@EnableJpaRepositories(basePackageClasses = UserRepository.class, enableDefaultTransactions = false)
@EnableTransactionManagement
@ImportResource({ "classpath:infrastructure.xml", "classpath:tx-manager.xml" })
static class Config {}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2015 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.data.jpa.repository.support;
import org.springframework.test.context.ContextConfiguration;
/**
* Integration tests for disabled default configurations via XML configuration.
*
* @author Oliver Gierke
* @soundtrack The Intersphere - Live in Mannheim
*/
@ContextConfiguration("disable-default-transactions.xml")
public class XmlConfigDefaultTransactionDisablingIntegrationTests extends DefaultTransactionDisablingIntegrationTests {}