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.
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.
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.
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.
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 ).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With