Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://static.springsource.org/spring/docs/current/reference/transaction.html#transaction-declarative-annotations" rel="noreferrer">http://static.springsource.org/spring/docs/current/reference/transaction.html#transaction-declarative-annotations</a></p> <blockquote> <p>In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual transaction at runtime even if the invoked method is marked with @Transactional.</p> <p>Consider the use of AspectJ mode (see mode attribute in table below) if you expect self-invocations to be wrapped with transactions as well. In this case, there will not be a proxy in the first place; instead, the target class will be weaved (that is, its byte code will be modified) in order to turn @Transactional into runtime behavior on any kind of method.</p> </blockquote> <p>The <code>@Autowired</code> reference <code>B b</code> (inside class <code>A</code>), is wrapped with a Spring AOP transaction-aware proxy. </p> <p>When <code>b.inner()</code> is called, you are invoking it on the transaction-aware instance, and it is marked as <code>@Transactional</code>. Thus a Spring managed transaction is started. </p> <p>When <code>b.outer()</code> is called, it is also on a transaction-aware instance, but it is <strong>not</strong> <code>@Transactional</code>. Thus a Spring managed transaction is <strong>not</strong> started.</p> <p>Once you are inside the invocation of <code>outer()</code> the call to <code>inner()</code> is <strong>not</strong> going through the transaction-aware proxy, it is being invoked directly. It is the same as <code>this.inner()</code>. Since you are invoking it directly, and not through the proxy, it does not have the Spring transaction-aware semantics. </p> <p>Since no transaction has been started, this results in the <code>TransactionRequiredException</code>.</p> <p>Possible solutions include making the method <code>outer()</code> <code>@Transactional</code>.</p> <pre><code> @Transactional public void outer() { inner(); } </code></pre> <p>Or making the entire class <code>@Transactional</code>.</p> <pre><code>@Transactional @Component class B { @PersistenceContext EntityManager em; </code></pre>
    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.
    3. 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