Skip to content

DATAJDBC-622 - Support transactionManagerRef in @EnableJdbcRepositories #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.springframework.context.annotation.Import;
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactoryBean;
import org.springframework.data.repository.config.DefaultRepositoryBaseClass;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

/**
* Annotation to enable JDBC repositories. Will scan the package of the annotated configuration class for Spring Data
Expand Down Expand Up @@ -121,4 +123,10 @@
*/
String dataAccessStrategyRef() default "";

/**
* Configures the name of the {@link DataSourceTransactionManager} bean definition to be used to create repositories
* discovered through this annotation. Defaults to {@code transactionManager}.
*/
String transactionManagerRef() default "transactionManager";

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import java.util.Optional;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactoryBean;
Expand All @@ -37,6 +38,8 @@
*/
public class JdbcRepositoryConfigExtension extends RepositoryConfigurationExtensionSupport {

private static final String DEFAULT_TRANSACTION_MANAGER_BEAN_NAME = "transactionManager";

/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationExtension#getModuleName()
Expand Down Expand Up @@ -78,6 +81,9 @@ public void postProcess(BeanDefinitionBuilder builder, RepositoryConfigurationSo
source.getAttribute("dataAccessStrategyRef") //
.filter(StringUtils::hasText) //
.ifPresent(s -> builder.addPropertyReference("dataAccessStrategy", s));

Optional<String> transactionManagerRef = source.getAttribute("transactionManagerRef");
builder.addPropertyValue("transactionManager", transactionManagerRef.orElse(DEFAULT_TRANSACTION_MANAGER_BEAN_NAME));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ static class DummyEntity {
@EnableJdbcRepositories(considerNestedRepositories = true,
includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = DummyRepository.class),
jdbcOperationsRef = "qualifierJdbcOperations", dataAccessStrategyRef = "qualifierDataAccessStrategy",
transactionManagerRef = "transactionManager",
repositoryBaseClass = DummyRepositoryBaseClass.class)
static class TestConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ NamedParameterJdbcOperations namedParameterJdbcTemplate() {
return new NamedParameterJdbcTemplate(dataSource);
}

@Bean
PlatformTransactionManager transactionManager() {
@Bean(name = "transactionManager")
PlatformTransactionManager defaultTransactionManager() {
return new DataSourceTransactionManager(dataSource);
}

Expand Down