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
Tomcy John
Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
Thanks, nice chapter.