Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are doing very bad things with threads and Hibernate sessions. First of all <code>CommunicationServiceImpl.sendAllMessages()</code> is transactional. This means <code>MessageToSend</code> entity is connected to Hibernate session while you iterate over <code>messagesToSend</code> collection. That's fine.</p> <p>However for each entity you start new thread<sup>*</sup> which processes that entity. Inside that thread you perform a lot of computations on <code>MessageToSend</code>, including some sub-queries and updates. This is where dangerous race condition manifests:</p> <p>If the thread started while we are still inside the main <code>while (iteratorMesToSe.hasNext())</code> loop (we are still inside <code>sendAllMessages()</code> method) <code>MessageToSend</code> is still attached to the original session started in parent thread. However if the parent thread completed iteration (we exited <code>sendAllMessages()</code>) but the child <code>MessageSender</code> is still running, you'll get transient errors as above. "<code>session closed</code>" basically means parent thread closed the session before the child thread finished processing.</p> <p>In simple words, you should never let objects bound to Hibernate session to escape from current thread. Instead detach them from session explicitly (e.g. by clearing the session or moving the transaction further up so that your objects are not attached to the session when you are iterating over them) and start new transaction for each processed row.</p> <p><sup>*</sup> - it seems like you are creating a new thread for each and every SMS you want to send. This is not very scalable, your system will fall once the total number of messages to send reaches several thousand. Instead use a thread pool (<a href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html" rel="nofollow"><code>ExecutorService</code></a>).</p>
    singulars
    1. This table or related slice is empty.
    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. 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