I'm using JPA repository in my SpringBoot app I have a background task that periodically set certain element in my database to 'ready' I also have an endpoint that a user can call and that can modify the one of the row of that same table.
Is there a way to avoid one and another to cancel each other writes? Take this scenario
Table:
Key
id name is_ready
0) Initial data there a key (1 no_name false)
1) the background task kicks in and is about to modify the table Key by setting
is_ready to true
Key key = repo.findKeyByIsReady(false)
key.setIsReady(true)
repo.save(key) <--- does NOT yet execute this
2) a user call the api endpoint to change a key name to "new_name" and complete
3) now the background service execute the repo.save(key) and the final data is
1 no_name true
instead of
1 new_name true
basically the background task has overwritten the key name set by the user
is there a way to avoid this kind of scenario? How are transaction going to be helpful here?