Spring Transaction Management
To some extent, Spring resolves the disadvantages of both global and local transactions. It enables application developers to use a consistent programming model in any environment. Developers write code once and can benefit from different transaction management strategies in different environments very easily. The Spring Framework provides both declarative and programmatic transaction management. Most users prefer declarative transaction management, which is recommended in most cases.
With programmatic transaction management, developers work with the Spring Framework transaction abstraction, which can run over any underlying transaction infrastructure. With the preferred declarative model, developers typically write little or no code related to transaction management, and hence do not depend on the Spring Framework transaction API, or any other transaction API.
Comparison with EJB
Before delving deep into the Spring transaction management, I would like to compare Spring framework to its counter-part, Enterprise Java Bean (EJB), to bring its differences and similarities.
- Support for both declarative and programmatic transaction management
- In doing declarative transaction, extensive choice for various transaction attributes
- Developer flexibility to choose underlying transaction strategy to be used in the application
- Support for JDBC, JTA, JDO, Hibernate etc.
- Doesn’t require and application server for its functioning
Clearly Spring brings all the advantages which EJB has and all the differences which Spring brings are advantages which makes it stand-out.
Advantages of Spring Transaction Management
We will now see the tangible advantages which the Spring transaction management brings into table which makes it right choice for use in your application development.
- Consistent programming model for different data access technologies and support technologies like JDBC, Hibernate, JPA and JTA.
Figure 9-2. Uniform API for transaction management in Spring
- Simpler API’s for programmatic trasanction management (agreed that this can be a very relative term, but it’s a general belief that the API’s are simpler). Intuitive and easy to use.
- Very good support for declarative trsandxtcin managenemt using two oprions namely:
- Full-fledged integration with Spring’s data access abstractions and Spring stuff.
Transaction Management Styles in Spring
The Spring framework provides an abstract transaction model that has the same API and semantics regardless of which underlying transaction technology you use. In order to switch from one technology to another, you generally have to alter your Spring configuration, but the idea is that you never needs to alter your business logic. That means, whether you’re using local, in-VM JDBC transactions or fully distributed, two-phase-commit XA JPA-style transactions, the API usage within your Spring code is the same.
Transaction management in Spring can be achieved by the following ways:
- Programmatic Transaction Management
- Using PlatformTransactionManager
- Using TransactionTemplate
- Declarative Transaction Management
- Java annotation-based
We will now discuss these transaction management supported by Spring in more details in the following sections of this Chapter.
Page Visitors: 8818