java - Concurrency issue in JPA -
we have deployed our application in 2 application servers(tomcat server) clustering environment. request coming 2 servers @ same time concurrency issue happening means it's creating duplicate entries in database. scenario : 1. request (transaction) update data 2. request (transaction) b update data @ same time 3. 2 row added table. among 1 duplicate.
let's have methods following signature
request a
updatedata() { //get entity manager //start transcation tx //persist //commit tx }
request b
updatedata() { //get entity manager //start transcation tx //persist //commit tx }
we want prevent scenarios this. request should persist data table. solve issue. #thanks environment details: jpa 2.1 , oracle database
you should use locking in database, if using typedquery
, should put in update methods:
typedquery<mytype> query = entitymanager.createquery("from mytype", mytype.class); query.setlockmode(lockmodetype.pessimistic_write);
this select update
, locks entity (database rows) until lock released - so, until end of transaction.
Comments
Post a Comment