Java Reference
In-Depth Information
The REQUIRES_NEW Propagation Behavior
Another common propagation behavior is REQUIRES_NEW . It indicates that the method must start a new
transaction and run within its new transaction. If there's an existing transaction in progress, it should be
suspended first (as, for example, with the checkout method on BookShopCashier , with a propagation of
REQUIRED ).
package com.apress.springenterpriserecipes.bookshop.spring;
...
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
public class JdbcBookShop extends JdbcDaoSupport implements BookShop {
...
@Transactional( propagation = Propagation.REQUIRES_NEW)
public void purchase(String isbn, String username) {
...
}
}
In this case, there will be three transactions started in total. The first transaction is started by
the checkout() method, but when the first purchase() method is called, the first transaction will be
suspended and a new transaction will be started. At the end of the first purchase() method, the
new transaction completes and commits. When the second purchase() method is called, another new
transaction will be started. However, this transaction will fail and roll back. As a result, the first book will
be purchased successfully while the second will not. Figure 4-4 illustrates the REQUIRES_NEW propagation
behavior.
Figure 4-4. The REQUIRES_NEW transaction propagation behavior
Search WWH ::




Custom Search