Spring Book – Chapter 9 – Transaction Management

Multiple Transaction Managers

If you have multiple transaction managers configured in your Spring configuration file as shown in Listing 9-10, you can use appropriate transaction managers to be applied on methods using the @Transactional annotation. Some changes are required while configuring the transaction managers in the configuration file and in annotation, we will have to qualify as to what is the appropriate transaction manager to be used. Listing 9-13 shows code sample for doing this.

Listing 9-13. Configuring multiple transaction managers and using in @Transactional annotation







public class LoyaltyService{


String doOperationOne(String attrib) {

//Some code….



String doOperationTwo(String attrib) {

//Some code….



Note: Annotation in the method level overrides the same annotation properties in the class level.

Figure 9-5. Spring transaction management in action

Figure 9-5 shows declarative transaction pictorially in all aspects. In this figure the “Client” calls the proxy either created by Spring AOP or AspectJ as the case may be, which actually start the transaction looking at the various “Advisors” configured and the method in the “Target Object” is invoked. After method execution, it goes back to the proxy, wherein the transaction is either committed or roll-backed. Once that is done the control is transferred back to the client.

Using Spring AOP

AOP enables us to encapsulate and separate cross-cutting concerns in your application. All the various operations related to transaction like beginning, ending, committing to database and rolling-back in case of exception, all fall into the various cross-cutting concerns in your application and AOP is the right candidate which can be used. There literally no change in the service class and callers. The only change is in the Spring bean configuration file. Listing 9-1 shows the changes in the Spring configuration file to achieve transaction management using Spring AOP.

Listing 9-14. Declarative Transaction management using Spring AOP


<!–—Transaction interceptor bean configuration —->


<!–—Transaction proxy creation —->



This is a very effective way of doing transaction management declaratively. In Listing 9-1 above, we have declared the proxy bean “loyaltyProxyService” which is an instance of “org.springframework.aop.framework.ProxyFactoryBean”. The target for this proxy will be the Loyalty Service object which is specified through “target” property. And because we want to intercept the transaction related operations, we have specified the interceptor “loyaltyTransactionInterceptor” through the element “interceptorNames”.

Page Visitors: 9675

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 *