简体   繁体   中英

How to avoid Spring Data Jpa Concurrency situation?

My project uses Spring Data JPA( JpaRepository ) for persistence operations. However when multiple requests are executed simultaneously, data is getting duplicated in a table where I want to add only unique records.

This happens because at first time, for all threads there is no matching record in that table andbhence it allows all threads to save record in that table.

Any suggestions how I can avoid it.

I saw Locking and Isolation as some of the suggested ways but I am not sure how we can implement them if we use JpaRepository interface methods.

This is nothing Spring Data is causing. The means of control for that are in the JPA and database areas. I suggest you read up on optimistic locking with JPA as well as unique indexes in databases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM