question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Spring boot jdbc data repository by multi data source

See original GitHub issue

please 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:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
oburgosmcommented, Jul 27, 2022

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/

1reaction
oburgosmcommented, Jul 28, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found