Transaction Management in Spring
Transaction Management :
Important aspect of transaction management is defining the right transaction boundary for e.g when should a transaction start,when should it end,when data should be committed in DB and when it should be rolled back (in the case of exception).
Spring provides the facility of AOP based transaction management in which transactions can be associated to the business logic as a concern.
Two types of transaction management are supported by the spring.
Declarative transaction management
Programmatic transaction management
- Programmatic transaction management : This means that you have to manage the transaction with the help of programming. That gives you extreme flexibility, but it is difficult to maintain.
- Declarative transaction management: This means you separate transaction management from the business code. You only use annotations or XML based configuration to manage the transactions.
Choosing between Programmatic and Declarative Transaction Management:
- Programmatic transaction management is good only if you have a small number of transactional operations. (Most of the times, this is not the case.)
- Transaction name can be explicitly set only using Programmatic transaction management.
- Programmatic transaction management should be used when you want explicit control over managing transactions.
- On the other hand, if your application has numerous transactional operations, declarative transaction management is worthwhile.
- Declarative Transaction management keeps transaction management out of business logic, and is not difficult to configure.
Components need to understand to use transaction management:
1. PlatformTransactionManager
It is an interface which provides method to start, commit, rollback transaction. Different Implementation of this interfacee are provided by the framework for the different API such as JDBC, JPA, JTA, Hibernate etc.
2. TransactionDefinition
It is an interface. It provides methods to obtain the characteristics of transaction. Different Implementation of TransactionManagement are provided by the framework.
3. TransactionStatus
It is also an interface. It provides the methods to obtain the state of the transaction. Its implementations are providing by the framework.
4. DataSourceUtils
It is a utility class. It facilities sharing of a connection among all the persistance method which participate in a transaction.
How It Works:
1. Transaction Manager is obtained.
2. TransactionDefinition object is created.
3. TransactionDefinition object is provided to the TransactionManager and the TransactionManager is asked to started the transaction.
4. TransactionManager reads the transaction characteristics from the TransactionDefinition.
5. TransactionManager asks a connection from the DataSourceUtils.
6. Connection is created by the DataSourceUtils.
7. Connection reference is provided to the TransactionManager as well as stored in a ThreadLocalRepository.
8. Using the connection, transaction is started by the TransactionManager and a TransactionStatus object is created to identify the transaction.
9. Reference of the TransactionStatus object is returned.
10 : Persistance methods are invoked.
11 : Connection is request by the persistance method from the DataSourceUtils.
12 : Connection participating in transaction is returned from the ThreadLocalRepository by the DataSourceUtils.
13 : Connection is used by the persistence methods for creating statements.
14 : Using the statement queries are executed.
15 : After the query execution control is returned to the DAO.
16 : DAO returns the control back to the business component without doing the connection.
17. Depending on the success or failure of the persistance mathod. TransactionManager is asked to commit and rollback the transaction.
18. TransactionManager identifies the connection for the transaction from the TransactionStatus.
19. TransactionManager asks the identfied connection to commit and rollaback the connection.
Comments
Post a Comment