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

javaspringspring-batch

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?

Best Answer

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.