We have a Spring Boot application with a controller class that is called from the clients and interacts with the DB. Based on a condition, the controller checks if a record for a unique value exists or not, if not, it creates a new record (with auto-generated primary key) and save it.
Now concurrency issue is in this condition:
Two different objects of class A
want an object of class B
with value b1
, both of them check at the same time in the DB, they get null
, now they try to create a new object of class B
with value b1
, here comes error.
I tried using, simple double-locking method,
final static Object lock = new Object();
public Object handle(cs cs, Map<String, Object> headers){
LOGGER.info(" persisting message : " + cs.getcsCode());
F f = repo.findByQuery();
if (f==null ) {
synchronized(lock) {
f = repo.findByQuery;
if (f==null ) {
repo.save(cs.getF1());
}
}
}
return csRepo.save(cs);
}
but later realized that my application runs on two JVMs, which use a common database. After going through some questions, I came to another option ,
@Transacational(isolation=REPEATABLE_READ)
But I am not sure if it will work in my scenario, as its explanation says, it is useful when modifying data on a row, but I am creating a new row. As this is a rare scenario and involves multiple JVMs, I can’t test it to be sure.