Spring boot jdbc data repository by multi data source
See original GitHub issueplease help me. I use multi data source in my project
data source properties:
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=db
spring.datasource.username=xxxxx
spring.datasource.password=xxxxx
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource2.url=jdbc:mysql://localhost:3306/db2
spring.datasource2.username=xxxx
spring.datasource2.password=xxx
spring.datasource2.driver-class-name=com.mysql.cj.jdbc.Driver
config class:
@Configuration
@EnableJdbcRepositories(jdbcOperationsRef = "mysqlNamedParameterJdbcOperations", basePackages = "com.example.demo.mysqlModels")
public class Config extends AbstractJdbcConfiguration {
@Bean("mysqlDataSource")
@ConfigurationProperties(prefix = "spring.datasource2")
public DataSource mysqlDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "mysqlNamedParameterJdbcOperations")
NamedParameterJdbcOperations mysqlNamedParameterJdbcOperations(@Qualifier("mysqlDataSource") DataSource mysqlDataSource) {
return new NamedParameterJdbcTemplate(mysqlDataSource);
}
}
@Configuration
@EnableJdbcRepositories(jdbcOperationsRef = "mssqlNamedParameterJdbcOperations", basePackages = "com.example.demo.mssqlModels")
public class Config2 extends AbstractJdbcConfiguration {
@Bean("mssqlDataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource mssqlDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "mssqlNamedParameterJdbcOperations")
NamedParameterJdbcOperations mssqlNamedParameterJdbcOperations(@Qualifier("mssqlDataSource") DataSource mssqlDataSource) {
return new NamedParameterJdbcTemplate(mssqlDataSource);
}
}
repository in com.example.demo.mssqlModels:
public interface MssqlRepository extends PagingAndSortingRepository<MyEntity, Integer> {}
repository in com.example.demo.mysqlModels:
public interface MysqlRepository extends PagingAndSortingRepository<MyEntity, Integer> {}
my service:
@Slf4j
@Service
public class MyService {
@Autowired
private final MssqlRepository mssqlRepository;
@Autowired
private final MysqlRepository mysqlRepository;
@PostConstruct
public void init() {
log.info("mssql result {}", mssqlRepository.findAll());
log.info("mysql result {}", mysqlRepository.findAll());
}
}
but result is same and both repositories read data from mysql datasource thanks
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Configure and Use Multiple DataSources in Spring Boot
But we sometimes need to access multiple databases. In this tutorial, we'll learn how to configure and use multiple data sources with Spring...
Read more >Multiple Data Sources with Spring Boot
Multiple Data Sources with Spring Boot · 1. Maven Setup · 2. DataSource Configurations · 3. JPA Entities · 4. Package Structure ·...
Read more >Configuring multiple data sources with Spring Boot 2 and ...
To configure multiple data sources in Spring Data JPA we need to group the Model classes and Repository interfaces for each data source...
Read more >How to Configure Multiple Data Sources in a Spring Boot ...
Learn how to configure multiple datasources using Spring Boot and Hiberanate. In this example, you will see how to configure 3 different ...
Read more >Spring Boot Application With Multiple Data sources - Dev Genius
To make a spring boot application that can communicate with more than one database you need to define some configuration to let spring...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I have this problem with SB 2.7.2
I have created a very simple sample project to reproduce the problem @philwebb
https://github.com/oburgosm/spring-data-jdbc-test/
Thanks for your response @wilkinsona . I just found that there is an issue to support multiple datasources in spring-data-jdbc, but it is still open. I’ll follow that issue.