Spring Book – Chapter 9 – Transaction Management

Multiple Transaction Managers

You have choice of declaring any number of transaction managers and then supplying to the transaction advices and according these will be applied by Spring transparently to the source code using Spring’s AOP. Listing 9-10 uses two transaction manager, JDBC and JPA. LoyaltyService class methods use the JDBC transaction management and CustomerService class methods use the JPA transaction management.

Listing 9-10. Multiple transaction manager usage in declarative transaction management

[xml]

<!–—JDBC transaction manager configuration (DataSource) – Start —->

<!–—End —->

<!–—JPA transaction manager configuration (Entity Manager) – Start—->

<!– Use Hibernate as JPA engine –>

<!–—End —->

[/xml]

Different transaction properties for different beans

Listing 9-10 shows different beans being applied with different transaction properties using multiple transaction advices. These transaction advices are used to configure the various transaction attributes.

Rolling back rules in a declarative transaction

It is possible to control rolling back rules using transaction advices, as shown in Listing 9-11 and Listing 9-12, with each example displaying a particular way of configuring the rolling back rule.

Listing 9-11. Rolling back transaction for a particular exception class namely “MyException”

[xml]

[/xml]

Listing 9-12. No rolling back of transaction for a particular exception class namely “MyOwnException”

[xml]

[/xml]

If multiple rolling-back and non-rolling-back rules are configured, the Spring framework evaluates all the rules and the strongest matching rule wins. A specific rule wins over generic rules similar to the catch block execution in the case of Java exception handling.

Page Visitors: 9642

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

1 thought on “Spring Book – Chapter 9 – Transaction Management

Leave a Reply

Your email address will not be published. Required fields are marked *