Declarative Transaction Management
Declarative transaction management is the most commonly used as it has the least impact on application code that you write. Declarative transaction management in Spring is made possible with Spring’s aspect-oriented programming (Spring AOP), which will be explained in detail in Chapter 20. Understanding of Spring AOP and its concepts is very much required to understand the various configurations explained in this section in detail. I will give adequate examples, it could be that you don’t understand everything at one go, but I would say that you go through the examples and later on when AOP is covered, come back and read this section once again to make sure that your understanding is complete in all aspects. Declarative transaction management can be configured in the following ways:
- XML-based
- Java annotation-based
- Using Spring AOP/AspectJ
Advantages of Declarative Transaction Management
Advantages which make declarative transaction management in Spring attractive to be used in your application development can be summarized as the following:
- Simple
- Non-invasive
- Handles most of the boilerplate code
- Commonly used
- Compared to programmatic, recommended method
XML-based
The XML declarative approach configures the transaction attributes in the Spring bean configuration file. Listing 9-9 shows one such bean configuration file in which Spring AOP is used to make all the methods in the LoyaltyService class in a transaction context.
Listing 9-9. A typical Spring configuration file using declarative transaction management of Spring using XML
[xml]<!–— Step – 1: DataSource bean declaration—->
<!–— Step – 2: Transaction Manager bean declaration (uses dataSource created in Step 1—->
<!–— Step – 3: Service bean declaration which will have methods in a transaction context—->
<!–— Step – 4: Transaction advice declaration —->
<!–— Step – 5: Uses AOP to configure transaction to all the methods in the service class, LoyaltyService —->
[/xml]
The <tx:advice/> definition makes the service object “loyaltyService” bean transactional and defines all methods starting with “methodStart” to execute in the context of a read-only transaction, and all other methods to execute with the default transaction semantics.
The <aop:config/> definition ensures that the transactional advice defined by the “txAdvice” bean executes at the appropriate points in the program.
The above Listing 9-9 configuration will be used to create a transactional proxy around the object that is created from the “loyaltService” bean definition. The proxy will be configured with the transactional advice, so that when an appropriate method is invoked on the proxy, a transaction is started, suspended, marked as read-only, and so on, depending on the transaction configuration associated. Doing this way your service class “LoyaltyService” doesn’t have to be modified at all. Figure 9-4 shows application of declarative transaction pictorially.
Figure 9-4. Declarative transaction management in action
Page Visitors: 9315


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.