Saturday 15 September 2012

java ee - Locking an entity -



java ee - Locking an entity -

i facing problem locking entity in jpa. have list of several entities database. each element there's edit button loads view editing entry. every time client tries edit entry, want check if entity locked preventing client load edit mask of entity.

my method loads entity:

[...] mail service = (emailkonto) query.getsingleresult(); system.out.println(getlock(mail).tostring()); setlock(mail, lockmodetype.pessimistic_write); system.out.println(getlock(mail).tostring());

the called methods:

public void setlock(t entity, lockmodetype lock) { getentitymanager().lock(entity, lock); } public lockmodetype getlock(t entity) { homecoming getentitymanager().getlockmode(entity); }

what happens first syso prints none, since there no lock applied. lock gets set , sec syso prints pessimistic_write. when refresh page or utilize tab / browser , click on edit button of same entity, first syso should print pessimistic_write, since never remove lock, 1 time again prints none. guys help me understand how implement kind of function?

regards

technically, pessimistic lock implemented via issuing select ... update command, locks row(s) current transaction, other select ... update update in other transaction fail.

all transaction-level locks released when transaction ended, hence such implementation not feasible functionality need, because need maintain transactions long client editing row. possible realize in heavy client (although terrible design), in web application not realistic because don't have feedback if client closes browser.

you need manually implement , manage locks @ business level, storing 'locks' in separate table. transaction-level (pessimistic) locks not designed function business-level locks. low level mechanism designed assert transaction consistency.

java-ee jpa glassfish locking pessimistic-locking

No comments:

Post a Comment