Tuesday, 15 January 2013

java - Detached entity passed to persist -



java - Detached entity passed to persist -

cacheid aci = new cacheid(); aci.setid(cacheobject.getid()); aci.setsequence(cacheobject.getsequence()); cache persisted = em.find(cache.class, aci); if (persisted == null){ system.out.println("========== persisting"); em.persist(cacheobject); }else{ system.out.println("========== merging"); em.merge(cacheobject); }

mean while im running project instance deletes entry current cacheid. need set new value database same cache id. when em.find() not homecoming null. (but in database entry deleted. end calling merge. , merge failed trying update 1 updated 0...

but if stop checking search , phone call persist "detached entity passed persist ". how can work around issue ?

you have problem of concurrent access , reply locking. if utilize optimistic locking in application, grab exception , retry (a limited number or times).

you can utilize pessimistic locking pattern using :

cache persisted = em.find(cache.class, aci, lockmodetype.pessimistic_write);

(or lockmodetype.pessimistic_read). lock obtained database, know if info has been removed. , 1 time lock obtained, no other transaction allowed removed it.

the remaining race status if 2 transaction seek create same data.

but beware : application should commit possible release lock avoid potential deadlocks or unnecessary blocking of other transactions.

java hibernate jpa

No comments:

Post a Comment