Faseeh Memon
Faseeh Memon

Reputation: 21

Preventing insertion of duplicate records on concurrent hits on RESTApi Stateless Application

I have spent days in finding a solution to a problem. I have a stateless spring boot application. take example of Payment (table) application using Spring boot. Following are the preventions and functionalities that I have to keep in mind while creating RESTApi.

Preventions

Required Functionality

I have tried other ways like multithreading or using synchronized method but this takes a lot time to process the request. I cannot keep user in waiting.

Basically I want to achieve a general solution for preventing concurrent requests at programming level.

Your Response will be highly appreciated.

Upvotes: 2

Views: 1787

Answers (1)

Gurkan İlleez
Gurkan İlleez

Reputation: 1573

You can use @Version annotation. You can use optimistic lock to avoid same row transaction commit to database.

@Entity
public class Entity {
   @Version
   long version;
}

But you have to add version column to database tables. If you can not use queue and you can not modify database tables. You have only one chance. Simply use redis or hazelcast distributed lock but it will cost you a lot.

Upvotes: 1

Related Questions