I have a POST API to create an entity called Person, and I check if person with the name and address exist or not, if not i create a person entity:
class Person {
UUID id
String name;
String address
}
public void createPerson(String name, String addr){
Person p = repository.findPersonByNameAndAddress(name, addr);
if (p != null) {
repository.create(name, addr);
}
}
If a client calls the POST with same data at the same time I will end up creating two person with same name and address (but different ID). How can i prevent this from happening? I am using spring boot + JPA/Hibernate + postgres
Thanks!