baomidou / baomidou/dynamic-datasource

调度spring batch job无法支持多数据源 默认使用主数据源

Open
#773 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
5.2k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

## Describe the bug
在Spring Batch Job环境中,`@DS("")`注解未能正确切换数据源,导致数据库操作仍然在默认的主数据源执行,而不是在预期的从数据源上执行。这导致了`Table doesn't exist`错误。

## To Reproduce
Reference code:
@Bean
public Tasklet syncCleanTasklet() {
return (contribution, chunkContext) -> {
testMapper.removeAll();
return RepeatStatus.FINISHED;
};
}

@Bean
public Step syncCleanStep(JobRepository jobRepository,
PlatformTransactionManager transactionManager) {
return new StepBuilder("syncCleanStep", jobRepository)
.tasklet(syncCleanTasklet(), transactionManager)
.build();
}

@Bean
public Job syncJob(JobRepository jobRepository,
Step syncCleanStep,
Step syncStep) {
return new JobBuilder("syncJob", jobRepository)
.start(syncCleanStep)
.next(syncStep)
.build();
}

@Mapper
@DS("starrocks")
public interface TestMapper{
   void removeAll();
}

执行报错:### Cause: java.sql.SQLSyntaxErrorException: Table 'db.test' doesn't exist

## Additional context
- 使用的技术栈:Spring Boot, Spring Batch, dynamic-datasource-spring-boot-starter
- 问题出现的环境:Spring Batch Job执行期间

Contributor guide

Open the contributing guide

Research direction

Start with the provided syncCleanTasklet, syncCleanStep, and TestMapper reproduction, then inspect how @DS("starrocks") is resolved during Spring Batch tasklet execution. Run the job and verify that removeAll() uses the starrocks datasource instead of the primary datasource and no longer reports the missing-table error.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.