hibernate - JPA merge() best practise -
i implemented service entity object
, uses pure jpa
, used spring
, in spring xml config
configured hibernate
jpa
impl. using spring
info crud
operations. in our scheme our entity
object beingness pulled/updated many times , there high contention(concurrency
) data. our code in many places many classes
inject
service bean , phone call getentity
method entity, after alter entity(which detached understanding, in same thread em object should same per knowledge) takes while entity comes service updated, phone call save()
method of service save entity. save()
method nil calls merge()
crud operation. transactional @transactional
annotation. here problem arises, when pulls entity object , while changing else might pull , alter , save back, entity read dirty , if save it, override updated entity. problem here changing entity outside of service , calling save back. here spring info repository classes dao layer.
optimistic lock
1 solution did not reasons, not work us. thinking pessimistic lock
. example, when entity update locking, alter somewhere else in different place , phone call back(the entity
locked against update!) work? not sure if still entitymanager
object there used pulling entity. if there takes quite long time pass 'smart' logics before gets updated , unlocks it.
here simple illustration code scenario:
class someentity { long id; string field1; string field2; string field3; string field4; string field5; string field6; string field7; //getters , setters, column annotations } class someentityserviceimple implemenet someentityservice{ @transactional save(someentity se){ //call spring data's merge() method } get(long id){ //call spring data's findone() } } //this class might spring bean, might not be. if not someentityservice bean shared appcontext class somecrazyclass{ @autowired someentityservice service; somemethod(){ someentity e = service.get(1l); //do tons of logic here, alter entity object, phone call methods , again, takes 3 seond service.save(e); //probably detached , updated object, overriding it. } } }
here cannot move tons of logic
within service layer, quite specific , kind of different logics in 100s of places.
so there way come around , @ to the lowest degree apply pessimistic lock situation?
thanks reading , answering .
for pessimistic locking, can seek below code
locking while fetching entity
entitymanager.find(entity.class, pk, lockmodetype.pessimistic_write);
applying lock afterwards
entitymanager.lock(entity, lockmodetype.pessimistic_write);
setting lock mode in query
query.setlockmode(lockmodetype.pessimistic_write);
else, can seek adding synchronized method in someentityserviceimpl
.
public synchronized void saveentity(e){ { object o = get(id); //-- fetch recent entity applychanges(o, e); //-- applying custom changes save(o); //-- persisting entity }
therefore, don't have move logic within service, delegate database operations. also, mutual place code alterations without affecting application logic.
hibernate jpa concurrency locking pessimistic-locking
No comments:
Post a Comment