Introduction
Programmatic transaction management approach allows you to manage the transaction with the help of programming in your source code. That gives you extreme flexibility, but it is difficult to maintain.
SEE MORE: Programmatic Transaction
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
Let us use PlatformTransactionManager directly to implement programmatic approach to implement transactions.
Have a instance of
TransactionDefinitionwith the appropriate transaction attributes.
For this example we will simply create an instance ofDefaultTransactionDefinitionto use the default transaction attributes.Once the
TransactionDefinitionis created, start transaction by callinggetTransaction()method, which returns an instance ofTransactionStatus.The
TransactionStatusobjects helps in tracking the current status of the transaction and finally,
if everything goes fine, usecommit()method of PlatformTransactionManager to commit the transaction,
otherwise userollback()to rollback the complete operation.
Code
StudentJDBCTemplate_programmatic.java
|
|
applicationContext_programmatic.xml
|
|