Skip to content
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

Spring boot jdbc data repository by multi data source #26579

Closed
davoodshiraz opened this issue May 17, 2021 · 6 comments
Closed

Spring boot jdbc data repository by multi data source #26579

davoodshiraz opened this issue May 17, 2021 · 6 comments
Labels
for: stackoverflow A question that's better suited to stackoverflow.com status: invalid An issue that we don't feel is valid

Comments

@davoodshiraz
Copy link

davoodshiraz commented May 17, 2021

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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 17, 2021
@philwebb
Copy link
Member

I've edited your comment to improve the formatting. You might want to check out this Mastering Markdown guide for future reference.

@philwebb
Copy link
Member

It's hard to tell from the description if this is a bug in Spring Boot, Spring Data JDBC or a configuration issue. You could try debugging your application to check that JdbcRepositoryFactoryBean.setJdbcOperations is called correctly. If you can't get anywhere with that, please provide a sample application as either a zip file or a GitHub project so that we can diagnose the problem.

@philwebb philwebb added the status: waiting-for-feedback We need additional information before we can continue label May 17, 2021
@wilkinsona
Copy link
Member

wilkinsona commented May 17, 2021

This has also been posted as a question on Stack Overflow where a couple of other people are also trying to help.

@davoodshiraz please don’t cross-post like this as it ends up in people duplicating effort and wasting their time. In the interests of avoid any more of that, I’m going to close this in favour of the post on Stack Overflow. Updating it with a minimal, reproducible example would make it easier for people to help. We can re-open this issue if it turns out that it is a Spring Boot bug.

@wilkinsona wilkinsona added for: stackoverflow A question that's better suited to stackoverflow.com status: invalid An issue that we don't feel is valid and removed status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged labels May 17, 2021
@oburgosm
Copy link

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/

@wilkinsona
Copy link
Member

@oburgosm Both of your repositories are sharing the same DataAccessStrategy that's defined as a bean in the context. This strategy is an instance of DefaultDataAccessStrategy which is using the @Primary NamedParameterJdbcOperations so all data access if being performed against the first data source.

You can configure a reference to a custom DataAccessStrategy on @EnableJdbcRepositories but defining one isn't entirely straightforward. I'm also not sure why you can configure a custom jdbcOperationsRef which is then ignored. jdbcOperationsRef also isn't mentioned in the reference documentation. In short, I'm getting a bit out of my depth here as this is specific to Spring Data JDBC and it out of Spring Boot's control. Please open a Spring Data JDBC issue so that they can take a look and hopefully make some improvements.

If you do open an issue, please comment here with a link to it so that people can follow along.

@oburgosm
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: stackoverflow A question that's better suited to stackoverflow.com status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

5 participants