I have a server function which is called by many clients, many times exactly at the same time. The server function does the following:
- get param1 from client
- creat object x (new objectx(param1))
- check if object x exists in db (jpa select query)
- if not exists add object x (jpa store entity)
- add y (jpa store entity)
This goes wrong when two or more clients run the function at the same time, multiple x objects get added to the database.
I simply solved this by creating a singleton manager class with a synchronized method which does the above.
Works nicely, cause now the function can only be called by one client at a time. (but i do get a problem when there are 2 servers, but that isn't the case yet)
But i was wondering is there a better way to solve this problem with jpa?