How to unlock one object under JPA

everything.

I wrote a program using pure JPA, whose provider was dormant, and the underlying database was Azure SQL DB. This program will work under a distributed environment, many of its copies will work on different servers.

I need a procedure like this (for ex-):
1, lock a specific student object,
2, check the status of the clas object, if the status is 1, then continue, still try to lock the class object, do not kill, get the lock of this class
3, check the status status (maybe changed by another thread from another server due to the distributed environment), if there is still no 1, then do something, change the status to 1,
4, open this class class
5, do other things (still hold the castle over the existing student entity)

I get a lock with the following seg code:

Map<String,Object> map = new HashMap<String, Object>(); map.put("javax.persistence.lock.timeout", 0); return this.getEntityManager().find( cls, id, LockModeType.PESSIMISTIC_WRITE,map); 

but I don’t know how to redo the lock in step 4, is there any method that allows you to simply lock the lock on the entity and remain other locks?

Thank you very much, any advice will be given.

+4
source share
1 answer

You must set the lock mode to none

  em.lock(myEntity, LockModeType.NONE); 

See http://www.objectdb.com/java/jpa/persistence/lock

+3
source

Source: https://habr.com/ru/post/1484958/


All Articles