Working With Spring Batch and Distributed Transaction

Kwong Hung Yip
The Startup
Published in
6 min readMar 4, 2020

--

Spring Batch is a great tool to deal with large data volume, the project introduces the chunk-oriented processing, that divides data into batches, for each of these batches, its transaction is independent to each other. In this post, I will show you how to work with Spring Batch and distributed transaction.

Distributed transaction is invoked in this example while the ItemWriter updates records in two independent MySQL databases, changes on these records must be commit or rollback at once. On the other hand, the Spring Batch library stores execution history and meta-data into another MySQL database, this database is isolated with the first two - the execution history should be saved even though the distributed transaction need to be rollback due to any exception.

All three MySQL databases mentioned above, plus the Spring Batch program are packed and available as a docker-compose stack, and all the source code in this example can be found in GitHub.

Highlighted techniques in this post

--

--