每个Spring Batch作业调用都会打开一个新的数据库连接池吗?

huangapple go评论92阅读模式
英文:

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(&quot;customerJob&quot;, Job.class);

storeRepository = ctx.getBean(StoreRepository.class);
customerRepository = ctx.getBean(CustomerRepository.class);

List&lt;Store&gt; stores = storeRepository.findAll();	


for (Store store: stores) {
    int customerCount = customerRepository.countCustomer(store.getStoreID(), reportDate);
    System.out.println(&quot;Count: &quot;+ customerCount);
    if (customerCount != 0) {
	JobParameters jobParameters = new JobParametersBuilder()
		.addString(&quot;reportDate&quot;, reportDate)
		.addString(&quot;ID&quot;, store.getStoreID())
		.toJobParameters();
	JobExecution jobExecution = jobLauncher.run(job, jobParameters);
	BatchStatus batchStatus = jobExecution.getStatus();
	System.out.println(&quot;Batch Status &quot; + batchStatus);
    }
    

}
}

Batch Job Configuration:

@Bean
public Step generateReport() throws Exception {
return this.stepBuilderFactory
	.get(&quot;generateReport&quot;)
	.&lt;Customer, Customer&gt;chunk(10)
	.reader(itemReader(null, null))
	.writer(itemWriter())
	.build();
}

@Bean
public Job customerReportJob() throws Exception {
return this.jobBuilderFactory
	.get(&quot;customerReportJob&quot;)
	.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&lt;Customer&gt; findByIDAndReportDate(String ID, String reportDate) {
String sqlQuery = &quot;SELECT * &quot; +  
	&quot;FROM CUSTOMER_REPORT &quot; +
	&quot;WHERE ID = &quot; +  ID + &quot;  &quot; +
	&quot;AND TO_CHAR(REPORT_DATE, &#39;MM/DD/YYYY&#39;) = &#39;&quot; + reportDate + &quot;&#39;&quot;;

return jdbcTemplate.query(sqlQuery, 
	(rs, rowNum) -&gt; 
	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.

huangapple
  • 本文由 发表于 2020年9月25日 10:43:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64057059.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定