0

I am working with Spring Batch job, I have a job which scheduled every minute and gets the latest transaction from the database, for the load balancing job scheduled on multiple machines for example. If Machine1 executing the job then the same job scheduled on the Machine2, I handled the unique transaction by Time, I stored the Min-Max time in the database while reading, So when the job gets scheduled on other it took the min-max time and execute the job based on time.

But I have one issue in this, Actually, I want to maintain the sequence of transaction based on customer Id, For example, if Machine1 Execute the transaction 1 to 100 and Machine2 will execute transaction 101 to 200 and the transaction 100 and 101 did by the same customer than transaction should be processed on the same sequence.

In My Case, Machine1 did not finish the task and at the same time Machine2 started its process, So the problem here is that the 101st transactions processed before 100th.  Check the execution flow in the below Image.enter image description here

shailendra kushwah
  • 117
  • 1
  • 1
  • 11

1 Answers1

0

I handled the unique transaction by Time, I stored the Min-Max time in the database while reading, So when the job gets scheduled on other it took the min-max time and execute the job based on time.

I would not implement my own transaction serialization logic, this is a non trivial problem. What you can do is use the job repository as a safeguard against duplicate job executions in a clustered environment. This option leverages the database capabilities on that when using an aggressive isolation level like SERIALIZABLE. A similar question here: Activate Batch on only one Server instance

In My Case, Machine1 did not finish the task and at the same time Machine2 started its process,

That should not be an issue if tasks are independent. The first rule of parallelism is to make each unit of work independent from others. So I would assign each job a different task to be able to run them in parallel on different machines without having to synchronize database access or deal with overlapping schedules.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50