-
Notifications
You must be signed in to change notification settings - Fork 41.8k
Closed
Labels
for: stackoverflowA question that's better suited to stackoverflow.comA question that's better suited to stackoverflow.comstatus: invalidAn issue that we don't feel is validAn issue that we don't feel is valid
Description
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.Driverconfig 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
Metadata
Metadata
Assignees
Labels
for: stackoverflowA question that's better suited to stackoverflow.comA question that's better suited to stackoverflow.comstatus: invalidAn issue that we don't feel is validAn issue that we don't feel is valid