spring-data-relational
spring-data-relational copied to clipboard
Multiple datasources support [DATAJDBC-321]
Marek Šabo opened DATAJDBC-321 and commented
As mentioned in the SO answer, spring-data-jdbc currently doesn't support multiple datasources. It would be great to add this support in future releases.
Immediate help for advanced spring users would be making JdbcRepositoryFactoryBean constructor public. That way we could provide our own dependencies based on the context of datasource.
Next step could extending the @EnableJdbcRepositories
annotation with refs/links to dependencies similar way the @EnableJpaRepositories does.
(In our multi-ds JPA project we use similar configuration to one outlined in this article: https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7)
Affects: 1.1 M1 (Moore), 1.0.4 (Lovelace SR4)
Reference URL: https://stackoverflow.com/a/50197148/303559
Issue Links:
- DATAJDBC-499 Register custom converters in two independent modules ("is duplicated by")
- DATAJDBC-622 Add support for transactionManagerRef in EnableJdbcRepository
14 votes, 15 watchers
AndreyChukhlebov commented
Jens Schauder Hello! I understand that the problem is not very common, but are there any improvements planned?)
problem solution for postgress https://github.com/AndreyChukhlebov/spring-data-mongo-web crutch)
@AndreyChukhlebov The solution worked perfectly! Thanks much!!!
Thanks to @AndreyChukhlebov, I've created an example for multiple RDB (MySQL & PostgreSQL) repositories. https://github.com/kota65535/spring-data-jdbc-multi-repos
We can use jdbcOperationsRef
, transactionManagerRef
and dataAccessStrategyRef
params of @EnableJdbcRepositories
to specify different dependent beans for repositories of each datasource.
But as for JdbcConverter
, which is the dependency of JdbcRepositoryFactoryBean
, currently we don't have an option to specify ref like above, and therefore we have to use the single JdbcConverter
bean for each repository. Due to this limitation, we cannot use multiple SQL dialects (MySQL, PostgreSQL, Oracle, etc...) at the same time.
Hi all!
I'm implementing the same solutions from this thread
- https://github.com/AndreyChukhlebov/spring-data-mongo-web
- https://github.com/kota65535/spring-data-jdbc-multi-repos
but experiencing these problems with JdbcRepositoryFactoryBean
:
Parameter 0 of method setConverter in org.springframework.data.jdbc.repository.support.JdbcRepositoryFactoryBean required a single bean, but 2 were found:
- db1JdbcConverter: defined by method 'db1JdbcConverter' in class path resource [.../JdbcDb1Config.class]
- db2JdbcConverter: defined by method 'db2JdbcConverter' in class path resource [.../JdbcDb2Config.class]
How do you solve this?
Spring Boot 2.5.2. Both datasources to Oracle.
Hi @SimSonic, your problem seems to be related to this issue, and you should annotate @Primary
to the one of the JdbcConverter
bean to specify which one you use like this.
Hi @SimSonic, your problem seems to be related to this issue, and you should annotate
@Primary
to the one of theJdbcConverter
bean to specify which one you use like this.
Thanks a lot. I've found both of my configurations were extending AbstractJdbcConfiguration. Removed that and marked some of beans @Primary
and it started to work.
I also encountered this problem. In which version is this problem solved?
@kota65535 thanks for the repo
Hi! Are there plans to implement full support for multiple data sources? This functionality is very lacking.
I'm trying to implement workaround solutions from @AndreyChukhlebov and @kota65535.
But I don't use Spring Boot, only spring-context
and spring-data-jdbc
.
How to implement this whithout Spring Boot?
@EnableAutoConfiguration(
exclude = {DataSourceAutoConfiguration.class, JdbcRepositoriesAutoConfiguration.class}
)
My sample project is here and description to it is here #1289
To specify a JdbcConverter for a particular repository, is there any reason I shouldn't override the repositoryFactoryBeanClass by doing the following?
@EnableJdbcRepositories(
repositoryFactoryBeanClass = MyRepositoryFactoryBean.class,
basePackages = "com.my.pacakges.repository",
transactionManagerRef = "myTransactionManager",
jdbcOperationsRef = "myJdbcOperations",
dataAccessStrategyRef = "myDataAccessStrategy"
)
public static class MyRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable>
extends JdbcRepositoryFactoryBean<T, S, ID> {
// constructor...
@Override
public void setConverter(JdbcConverter notUsed) {
JdbcConverter myConverter = MyConfig.getBean("myJdbcConverter", JdbcConverter.class);
super.setConverter(myConverter);
}
}
I also use this same method to specify the dialect, as it seems to pick up the wrong dialect even though it is set correctly in the dataAccessStrategy.
This works well for me, but I'm just concerned about what the other consequences might be.