title: Locking JPA entity hierarchy being updated massively from MDB's in cluster
body: <p>I need some help in the following situation: imagine a hierarchy of <code>Job</code> entities that have a <code>progress</code> attribute. Some jobs consist of multiple subjobs making a job tree. The progress of these composite jobs is calculated from their subjobs. The bottom jobs progresses are updated periodically and then the whole tree's progress is recalculated bottom-up. Job progresses are updated via JMS messages received. In this case the job is fetched via JPA from the database and the progress is modified and a recursive recalculation is started.</p>
  
  <p>How should I deal with locking if this runs in a cluster? I would like to avoid situations when two subjobs are updated from 0% to 100% and the parent job goes to 50% instead of 100% as both the updates see 0%, 100% and vica versa.</p>
  
  <p>My first thought was using synchronization on the job objects. But this is not ok, because multiple runtime objects may represent the same record in the database.</p>
  
  <p>Could you suggest me a good, efficient way to handle this situation?</p>