Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring batch use different transaction-manager between job-repository and actual tasks

I am using Spring Batch using 2 (maybe more, assume 2 for simplicity) databases. One for storing all the job datas(all the BATCH_* tables). The other for actually running my business logic data. There are some things I dont quite understand.

  1. When I declared my JobRepository I have already specified my TransactionManager, why do I have to do it again on my tasklet?(I am not using the default name on purpose)
  2. I am currently giving tasklet the same TrasactionManager as my JobRepository which is manages different connections from what I am doing inside my steps. Does that mean I have do my own transaction management inside my writer or reader?
  3. If #2 is true, the "What If" in How does Spring Batch transaction management work? will be a problem for me (without doing XA) right? Because my data and job data are in different connections, right?
like image 721
lunaspeed Avatar asked Sep 15 '15 01:09

lunaspeed


People also ask

Can I use multiple transaction managers within a Spring application?

Can I use multiple transaction managers within a spring application? Yes. @Transactional annotation has the property transactionManager which can be set to specify which transaction manager to be used.

How do I manage transactions in Spring Batch?

Spring Batch handles transactions at the step level. This means that Spring Batch will never use only one transaction for a whole job (unless the job has a single step). Remember that you're likely to implement a Spring Batch job in one of two ways: using a tasklet or using a chunk-oriented step.

How can we share data between the different steps of a job in Spring Batch?

Passing data between steps. In Spring Batch, ExecutionContext of execution context that can be used in the scope of each step and job is provided. By using the execution context, data can be shared between the components in the step.

What is Resourceless Transaction Manager?

ResourcelessTransactionManager is a No-Op implementation of PlatformTransactionManager which means there will be no real transaction ongoing against a transactional resource (hence the term Resourceless ).


1 Answers

You can use a separate Transaction manager for tasks and the job repository. See here: http://forum.spring.io/forum/spring-projects/batch/39609-using-2-different-datasources-with-spring-batch

Thus, the need to designate two of them.

That being said, even though you can use two separate transaction managers, it doesn't mean that you should. If you don't go XA, then imagine what would happen if a business process ran successfully, but the job data wasn't saved. The next time the batch process ran, Spring Batch would think the job failed, and try to run it again.

like image 104
Brad Avatar answered Oct 10 '22 21:10

Brad