Java Reference
In-Depth Information
How It Works
Transaction propagation happens when a transactional method is called by another method. For
example, suppose that a customer would like to check out all books to purchase at the book shop
cashier. To support this operation, you define the
Cashier
interface as follows:
package com.apress.springenterpriserecipes.bookshop.spring;
...
public interface Cashier {
public void checkout(List<String> isbns, String username);
}
You can implement this interface by delegating the purchases to a book shop bean by calling its
purchase()
method multiple times. Note that the
checkout()
method is made transactional by applying
the
@Transactional
annotation.
package com.apress.springenterpriserecipes.bookshop.spring;
...
import org.springframework.transaction.annotation.Transactional;
public class BookShopCashier implements Cashier {
private BookShop bookShop;
public void setBookShop(BookShop bookShop) {
this.bookShop = bookShop;
}
@Transactional
public void checkout(List<String> isbns, String username) {
for (String isbn : isbns) {
bookShop.purchase(isbn, username);
}
}
}
Then define a cashier bean in your bean configuration file and refer to the book shop bean for
purchasing books.
<bean id=
"
cashier
"
class=
"
com.apress.springenterpriserecipes.bookshop.spring.BookShopCashier
"
>
<property name=
"
bookShop
"
ref=
"
bookShop
"
/>
</bean>
To illustrate the propagation behavior of a transaction, enter the data shown in Tables 4-7, 4-8, and
4-9 in your
bookshop
database.
Search WWH ::
Custom Search