Peter
Peter

Reputation: 189

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.

Thx.

Regards, Petar

Upvotes: 0

Views: 372

Answers (1)

Gurkan İlleez
Gurkan İlleez

Reputation: 1573

You can use optimistic lock and avoid overriding of row.

public class A {

   @Id
   private String id;

   @Version
   private Long version;

}

@Version annotation will cover optimistic lock for you. For More detail : https://www.baeldung.com/jpa-optimistic-locking

Upvotes: 1

Related Questions