英文:
Does every job call in Spring Batch open a new database connection pool?
问题
以下是您提供的内容的翻译部分:
主要方法(为每个商店运行作业):
public static void main(String[] args) throws JobExecutionAlreadyRunningException, JobRestartException,
JobInstanceAlreadyCompleteException, JobParametersInvalidException {
SpringApplication app = new SpringApplication(MainApp.class);
ConfigurableApplicationContext ctx = app.run(args);
JobLauncher jobLauncher = ctx.getBean(JobLauncher.class);
Job job = ctx.getBean("customerJob", Job.class);
storeRepository = ctx.getBean(StoreRepository.class);
customerRepository = ctx.getBean(CustomerRepository.class);
List<Store> stores = storeRepository.findAll();
for (Store store: stores) {
int customerCount = customerRepository.countCustomer(store.getStoreID(), reportDate);
System.out.println("Count: " + customerCount);
if (customerCount != 0) {
JobParameters jobParameters = new JobParametersBuilder()
.addString("reportDate", reportDate)
.addString("ID", store.getStoreID())
.toJobParameters();
JobExecution jobExecution = jobLauncher.run(job, jobParameters);
BatchStatus batchStatus = jobExecution.getStatus();
System.out.println("Batch Status " + batchStatus);
}
}
}
批处理作业配置:
@Bean
public Step generateReport() throws Exception {
return this.stepBuilderFactory
.get("generateReport")
.<Customer, Customer>chunk(10)
.reader(itemReader(null, null))
.writer(itemWriter())
.build();
}
@Bean
public Job customerReportJob() throws Exception {
return this.jobBuilderFactory
.get("customerReportJob")
.start(generateReport())
.build();
}
仓库:
@Repository
public class CustomerRepositoryImpl implements CustomerRepository {
private final JdbcTemplate jdbcTemplate;
@Autowired
public CustomerRepositoryImpl(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public List<Customer> findByIDAndReportDate(String ID, String reportDate) {
String sqlQuery = "SELECT * " +
"FROM CUSTOMER_REPORT " +
"WHERE ID = " + ID + " " +
"AND TO_CHAR(REPORT_DATE, 'MM/DD/YYYY') = '" + reportDate + "'";
return jdbcTemplate.query(sqlQuery,
(rs, rowNum) ->
new Customer(mapper(rs)));
}
}
application.properties:
# Oracle 设置
spring.datasource.url=##
spring.datasource.username=##
spring.datasource.password=##
spring.datasource.driver-class-name=##
# Hikari
spring.datasource.hikari.maximum-pool-size=5
spring.datasource.hikari.minimumIdle=5
spring.datasource.hikari.idleTimeout=30000
logging.level.com.zaxxer.hikari=debug
# Spring Batch
spring.batch.job.enabled=false
英文:
I'm querying rows from a database using JDBC template and for each rows, I'm programmatically running a job which executes a chunk-based step. I'm worried that each job call will create a new set of connection pool. Does it create a new set of connection pool or just connect to the current pool?
Main Method (Runs the job for each stores):
public static void main(String[] args) throws JobExecutionAlreadyRunningException, JobRestartException,
JobInstanceAlreadyCompleteException, JobParametersInvalidException {
SpringApplication app = new SpringApplication(MainApp.class);
ConfigurableApplicationContext ctx = app.run(args);
JobLauncher jobLauncher = ctx.getBean(JobLauncher.class);
Job job = ctx.getBean("customerJob", Job.class);
storeRepository = ctx.getBean(StoreRepository.class);
customerRepository = ctx.getBean(CustomerRepository.class);
List<Store> stores = storeRepository.findAll();
for (Store store: stores) {
int customerCount = customerRepository.countCustomer(store.getStoreID(), reportDate);
System.out.println("Count: "+ customerCount);
if (customerCount != 0) {
JobParameters jobParameters = new JobParametersBuilder()
.addString("reportDate", reportDate)
.addString("ID", store.getStoreID())
.toJobParameters();
JobExecution jobExecution = jobLauncher.run(job, jobParameters);
BatchStatus batchStatus = jobExecution.getStatus();
System.out.println("Batch Status " + batchStatus);
}
}
}
Batch Job Configuration:
@Bean
public Step generateReport() throws Exception {
return this.stepBuilderFactory
.get("generateReport")
.<Customer, Customer>chunk(10)
.reader(itemReader(null, null))
.writer(itemWriter())
.build();
}
@Bean
public Job customerReportJob() throws Exception {
return this.jobBuilderFactory
.get("customerReportJob")
.start(generateReport())
.build();
}
Repository:
@Repository
public class CustomerRepositoryImpl implements CustomerRepository {
private final JdbcTemplate jdbcTemplate;
@Autowired
public CustomerRepositoryImpl(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public List<Customer> findByIDAndReportDate(String ID, String reportDate) {
String sqlQuery = "SELECT * " +
"FROM CUSTOMER_REPORT " +
"WHERE ID = " + ID + " " +
"AND TO_CHAR(REPORT_DATE, 'MM/DD/YYYY') = '" + reportDate + "'";
return jdbcTemplate.query(sqlQuery,
(rs, rowNum) ->
new Customer(mapper(rs)));
}
}
application.properties:
# Oracle settings
spring.datasource.url=##
spring.datasource.username=##
spring.datasource.password=##
spring.datasource.driver-class-name=##
# Hikari
spring.datasource.hikari.maximum-pool-size=5
spring.datasource.hikari.minimumIdle=5
spring.datasource.hikari.idleTimeout=30000
logging.level.com.zaxxer.hikari=debug
# Spring Batch
spring.batch.job.enabled=false
答案1
得分: 0
根据您的配置,您的作业将使用相同的数据源/连接池。您可以通过查看日志来确认这一点。
英文:
According to your configuration, your jobs will use the same datasource/connection pool. You should be able to confirm that by looking at the logs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论