Skip to content

Db insertion row if not exist spring boot

I have a two instances of one spring boot microservice. If two requests are sent at the same time, and the second request can update the row that was created by the first request. How i can prevent this? For example the table contains this columns :id,format,username,groupName and if a row exists (3,”test”,”test”,”test”) and if we try to insert another row with same groupName and format,the insertion should fail.


Regards, Petar


You can use optimistic lock and avoid overriding of row.

public class A {

   private String id;

   private Long version;


@Version annotation will cover optimistic lock for you. For More detail :