We need to allow the users to import the huge catalog into the application. How do we achieve this with Spring batch as the Job is singleton in Spring Batch. How do we tweak it so that I can invoke the same job any number of times with thread safety. We are fine with synchronous processing and not looking for Async .Appreciate your inputs.
-
What have you tried? Are you trying to run steps in parallel and running into threading issues? – Charlie Feb 01 '16 at 20:55
-
I haven't implemented it. But after going through different posts, I want to know ,since the Job is singleton, how do we make sure every user gets a separate instance of the same job. Or is there any way we caan achieve this with singleton Job. – Srinivas Feb 01 '16 at 21:00
-
My understanding is the every job is a separate instance by default. What makes you think otherwise? What posts have you looked at? – Charlie Feb 01 '16 at 21:04
-
If I have an ImportJob for importing my huge catalogs into DB and many providers can import at the same time synchronously, do we see any thread safety if I change the Job parameter every time, eg. current timestamp ? Am I clear? – Srinivas Feb 01 '16 at 21:07
2 Answers
Even though the job configuration is a singleton, each job instance is created from the job configuration as a new object by the job launcher, so you should have no problems with concurrency.

- 146
- 9
It sounds like multiple updates are going to be happening in an unsafe way to your database. E.g. if you have table 1 row 1 being updated by Job1 and another user kicks off Job2 there's no guarantee what values you'll end up with in row 1. I wouldn't be concerned about thread safety so much as row level concurrency safety. Typically if you only want a single import to run at a time the solution is not something like Spring, but a database specific import tool.
UPDATE:
See this SO answer for how to customize Spring Batch to only allow one job to run at at time. Note - this has nothing to do with thread safety. This is not how Spring Batch is typically used which is why this isn't listed as a normal use case in their docs.
-
Every user will import the catalog specific to their business. It means they will insert or update data only for their business. And we can make sure only one user from each business can invoke the job at a time.Does it make sense? Or do we see issues still? – Srinivas Feb 02 '16 at 03:35
-
Your requirement makes sense - you don't want more than one user updating a table at a time. Spring Batch does not provide this out of the box, but it shouldn't be hard to do see link above. – Charlie Feb 02 '16 at 14:00