Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Generally, as said in the comments, transactions shouldn't be run in multiple threads. However, there are cases, where it is acceptable.</p> <ul> <li>you need to make some asynchronous communication with a web-service (without making the user wait for the result), and store the result when it comes</li> <li>you need read-only transactions in the multiple threads.</li> </ul> <p>If you create your thread using <code>new</code>, it is not part of the spring context. Hence, when the method creating the thread finishes, your transaction interceptor will close the transaction (and session, eventually), and you will get the above exception.</p> <p>(For more details - <a href="http://static.springsource.org/spring/docs/2.5.x/reference/beans.html" rel="nofollow noreferrer">Spring docs</a>, see "Lookup injection")</p> <p>You need to create your threads within the spring context. And since you are probably creating them from a <code>singleton</code> bean, it is the rare case of creating <code>prototype</code> beans from a <code>singleton</code> bean. So in order to create a thread in the spring context, you can use:</p> <pre><code> &lt;bean id="mainBean" class="com.my.MyClass"&gt; &lt;lookup-method name="createThread" bean="myThreadBean"/&gt; &lt;/bean&gt; </code></pre> <p>You should also map your <code>ThreadtestRunnable</code> class in the <code>applicationContext.xml</code> or annotate it as <code>@Component("myThreadBean")</code>.</p> <p>Then define an <code>abstract</code> method on your main bean named <code>createThread</code> and returning your thread class. Annotate your run method with <code>@Transactional</code> (or define the appropriate aop rules), and try it. Perhaps you will need to set <code>propagation=Propagation.REQUIRES_NEW"</code> in your <code>@Transactional</code>. If anything is wrong, get back here.</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. 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