Declarative transaction management approach allows you to manage the transaction with the help of configuration instead of hard coding in your source code. This means that you can separate transaction management from the business code.
You only use annotations or XML based configuration to manage the transactions. The bean configuration will specify the methods to be transactional.
Declarative transaction VS Programmatic Transaction
Declarative transaction management is preferable over programmatic transaction management though it is less flexible than programmatic transaction management, which allows you to control transactions through your code. But as a kind of crosscutting concern, declarative transaction management can be modularized with the AOP approach. Spring supports declarative transaction management through the Spring AOP framework.
Steps
Here are the steps associated with declarative transaction:
We use <tx:advice /> tag, which creates a transaction-handling advice and same time we define a pointcut that matches all methods we wish to make transactional and reference the transactional advice.
If a method name has been included in the transactional configuration then created advice will begin the transaction before calling the method.
Target method will be executed in a try / catch block.
If the method finishes normally, the AOP advice commits the transaction successfully otherwise it performs a rollback.