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

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

Does every job call in Spring Batch open a new database connection pool?

问题

以下是您提供的内容的翻译部分:

主要方法(为每个商店运行作业):

  1. public static void main(String[] args) throws JobExecutionAlreadyRunningException, JobRestartException,
  2. JobInstanceAlreadyCompleteException, JobParametersInvalidException {
  3. SpringApplication app = new SpringApplication(MainApp.class);
  4. ConfigurableApplicationContext ctx = app.run(args);
  5. JobLauncher jobLauncher = ctx.getBean(JobLauncher.class);
  6. Job job = ctx.getBean("customerJob", Job.class);
  7. storeRepository = ctx.getBean(StoreRepository.class);
  8. customerRepository = ctx.getBean(CustomerRepository.class);
  9. List<Store> stores = storeRepository.findAll();
  10. for (Store store: stores) {
  11. int customerCount = customerRepository.countCustomer(store.getStoreID(), reportDate);
  12. System.out.println("Count: " + customerCount);
  13. if (customerCount != 0) {
  14. JobParameters jobParameters = new JobParametersBuilder()
  15. .addString("reportDate", reportDate)
  16. .addString("ID", store.getStoreID())
  17. .toJobParameters();
  18. JobExecution jobExecution = jobLauncher.run(job, jobParameters);
  19. BatchStatus batchStatus = jobExecution.getStatus();
  20. System.out.println("Batch Status " + batchStatus);
  21. }
  22. }
  23. }

批处理作业配置:

  1. @Bean
  2. public Step generateReport() throws Exception {
  3. return this.stepBuilderFactory
  4. .get("generateReport")
  5. .<Customer, Customer>chunk(10)
  6. .reader(itemReader(null, null))
  7. .writer(itemWriter())
  8. .build();
  9. }
  10. @Bean
  11. public Job customerReportJob() throws Exception {
  12. return this.jobBuilderFactory
  13. .get("customerReportJob")
  14. .start(generateReport())
  15. .build();
  16. }

仓库:

  1. @Repository
  2. public class CustomerRepositoryImpl implements CustomerRepository {
  3. private final JdbcTemplate jdbcTemplate;
  4. @Autowired
  5. public CustomerRepositoryImpl(JdbcTemplate jdbcTemplate) {
  6. this.jdbcTemplate = jdbcTemplate;
  7. }
  8. @Override
  9. public List<Customer> findByIDAndReportDate(String ID, String reportDate) {
  10. String sqlQuery = "SELECT * " +
  11. "FROM CUSTOMER_REPORT " +
  12. "WHERE ID = " + ID + " " +
  13. "AND TO_CHAR(REPORT_DATE, 'MM/DD/YYYY') = '" + reportDate + "'";
  14. return jdbcTemplate.query(sqlQuery,
  15. (rs, rowNum) ->
  16. new Customer(mapper(rs)));
  17. }
  18. }

application.properties:

  1. # Oracle 设置
  2. spring.datasource.url=##
  3. spring.datasource.username=##
  4. spring.datasource.password=##
  5. spring.datasource.driver-class-name=##
  6. # Hikari
  7. spring.datasource.hikari.maximum-pool-size=5
  8. spring.datasource.hikari.minimumIdle=5
  9. spring.datasource.hikari.idleTimeout=30000
  10. logging.level.com.zaxxer.hikari=debug
  11. # Spring Batch
  12. 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):

  1. public static void main(String[] args) throws JobExecutionAlreadyRunningException, JobRestartException,
  2. JobInstanceAlreadyCompleteException, JobParametersInvalidException {
  3. SpringApplication app = new SpringApplication(MainApp.class);
  4. ConfigurableApplicationContext ctx = app.run(args);
  5. JobLauncher jobLauncher = ctx.getBean(JobLauncher.class);
  6. Job job = ctx.getBean(&quot;customerJob&quot;, Job.class);
  7. storeRepository = ctx.getBean(StoreRepository.class);
  8. customerRepository = ctx.getBean(CustomerRepository.class);
  9. List&lt;Store&gt; stores = storeRepository.findAll();
  10. for (Store store: stores) {
  11. int customerCount = customerRepository.countCustomer(store.getStoreID(), reportDate);
  12. System.out.println(&quot;Count: &quot;+ customerCount);
  13. if (customerCount != 0) {
  14. JobParameters jobParameters = new JobParametersBuilder()
  15. .addString(&quot;reportDate&quot;, reportDate)
  16. .addString(&quot;ID&quot;, store.getStoreID())
  17. .toJobParameters();
  18. JobExecution jobExecution = jobLauncher.run(job, jobParameters);
  19. BatchStatus batchStatus = jobExecution.getStatus();
  20. System.out.println(&quot;Batch Status &quot; + batchStatus);
  21. }
  22. }
  23. }

Batch Job Configuration:

  1. @Bean
  2. public Step generateReport() throws Exception {
  3. return this.stepBuilderFactory
  4. .get(&quot;generateReport&quot;)
  5. .&lt;Customer, Customer&gt;chunk(10)
  6. .reader(itemReader(null, null))
  7. .writer(itemWriter())
  8. .build();
  9. }
  10. @Bean
  11. public Job customerReportJob() throws Exception {
  12. return this.jobBuilderFactory
  13. .get(&quot;customerReportJob&quot;)
  14. .start(generateReport())
  15. .build();
  16. }

Repository:

  1. @Repository
  2. public class CustomerRepositoryImpl implements CustomerRepository {
  3. private final JdbcTemplate jdbcTemplate;
  4. @Autowired
  5. public CustomerRepositoryImpl(JdbcTemplate jdbcTemplate) {
  6. this.jdbcTemplate = jdbcTemplate;
  7. }
  8. @Override
  9. public List&lt;Customer&gt; findByIDAndReportDate(String ID, String reportDate) {
  10. String sqlQuery = &quot;SELECT * &quot; +
  11. &quot;FROM CUSTOMER_REPORT &quot; +
  12. &quot;WHERE ID = &quot; + ID + &quot; &quot; +
  13. &quot;AND TO_CHAR(REPORT_DATE, &#39;MM/DD/YYYY&#39;) = &#39;&quot; + reportDate + &quot;&#39;&quot;;
  14. return jdbcTemplate.query(sqlQuery,
  15. (rs, rowNum) -&gt;
  16. new Customer(mapper(rs)));
  17. }
  18. }

application.properties:

  1. # Oracle settings
  2. spring.datasource.url=##
  3. spring.datasource.username=##
  4. spring.datasource.password=##
  5. spring.datasource.driver-class-name=##
  6. # Hikari
  7. spring.datasource.hikari.maximum-pool-size=5
  8. spring.datasource.hikari.minimumIdle=5
  9. spring.datasource.hikari.idleTimeout=30000
  10. logging.level.com.zaxxer.hikari=debug
  11. # Spring Batch
  12. 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:

确定