Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Artem,</p> <p>Can you briefly explain what is it you're trying to achieve? Bird's eye view, obviously - like is this (invoked from) UI code? Or is it server-side process (so you'd have direct controls over threads)? The reason I ask is I get a (perhaps incorrect) feeling from this and your other related questions that you're trying to use optimistic locking for something it wasn't designed for and that's what's causing all the trouble.</p> <p>As far as <code>StaleObjectStateException</code> goes, it is most definitely thrown from both <code>DefaultFlushEventListener</code> and <code>AutoFlushEventListener</code> that handle explicit / implicit flushes. Are you invoking flush() manually? If not, perhaps the exception is being caught / logged by wrapper code around auto flushing (Spring? TransactionManager? EntityManager?)</p> <p><strong>Update</strong></p> <p>Thank you for clarifying the question. I'm still a bit unclear about whether you want to prevent multiple <strong>threads</strong> from <i>concurrently modifying</i><sup>1</sup> the same entity or prevent multiple <strong>users</strong> from trying to concurrently edit it.</p> <p>Former scenario can be handled via optimistic locking; however without explicit <code>flush()</code> it becomes non-deterministic (thread that made the modification first may not be flushed / committed first). See my answer to <a href="https://stackoverflow.com/questions/1399679/can-hibernates-version-consider-changes-in-related-entities">this question</a> for more details. Another problem with automatic flush is what you're currently experiencing - failed version check is not discovered until flush and, if that flush coincides with attempt to commit transaction, exception thrown is RollbackException. Either way, entire transaction is rolled back. </p> <p>Latter scenario (preventing users from editing) can NOT be handled by optimistic locking. You will need to implement pessimistic locking instead - either at the database or application level. In other words, the process is:</p> <ol> <li>user wants to edit entity</li> <li>check if lock exists on entity</li> <li>YES - prohibit editing (allow read-only view?)</li> <li>NO - lock entity, allow editing</li> <li>commit (cancel) changes; release lock</li> </ol> <p>Be sure to expire existing locks after a certain period of user inactivity if you go with this approach.</p> <hr> <p><i>concurrently modifying</i><sup>1</sup> is not really accurate in this case (that's what transactions are for); we're talking about preventing one thread from overwriting edits of another based on older version.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload