Declarative Transaction Management Example in Spring

This tutorial shows an example on declarative transaction management in Spring. The declarative transaction management example uses two approaches – annotation based and XML based configurations. You can use as per your needs.

For introduction on Spring Transaction please go through Spring Transaction
Please follow the steps

Create a table in MySQL

 

Dump some data

 

Annotation based

Add <context:annotation-config/> to applicationContext.xml file to tell Spring to read @Transactional annotation
Add <tx:annotation-driven/> to applicationContext.xml file to wrap your code in transaction scope
Initialize the DataSourceTransactionManager bean in applicationContext.xml file

WEB-INF/jdbc.properties

 

WEB-INF/applicationContext.xml

 

I have put @Transactional annotation at the Service layer that knows about a unit of work because you may have several DAO classes, injected into a Service, that need to work together in a single transaction.

In the following Service class we have @Transactional annotation for declarative transaction. We can put put @Transactional annotation at class level or at method level. If we put @Transactional annotation at class level then it wraps all the method in a transaction scope. If we put @Transactional annotation at method level then it works with the particular method on which it is applied. We can put @Transactional annotation at class and at method level at the same time and in this case @Transactional annotation will be applied for all the methods with similar behaviour except the methods for which you have applied @Transactional annotation at method level will have different behaviour. @Transactional annotation has several properties such as readOnly, isolation, propagation, rollbackFor, noRollbackFor etc. These properties can be used to control the behaviour and communication with other transactions. We can use @Transactional(readOnly=true) for select queries and @Transactional for insert and update queries because by default readOnly=false. Another two important properties are rollbackFor and noRollbackFor – used for if a class throws exception the transaction will be rolled back for that exception class and rollback will not happen for that exception class respectively. I have written deleteItem() to throw an Exception so that I can show you how the data is rolled back on Exception.

Service class

 

 

DAO Class

 

I have thrown new Exception() from deleteItem() even after deletion of an item to show you that the data gets rolled back on Exception

 

Model class

 

JDBC utility classes

 

 

Test the Spring Transaction

 

XML configuration based

We have to change only in applicationContext.xml and ItemServiceImpl.java

Initialize the DataSourceTransactionManager bean in applicationContext.xml file as shown for the Annotation based configuration.
Add <tx:advice /> to applicationContext.xml file to configure the transaction advice. Vaarious transaction properties can be configured for different methods. So now instead of @Transactional(readOnly = true) we will write <tx:method name=”get*” read-only=”true” />. For rollback on Exception we write <tx:method name=”delete*” rollback-for=”java.lang.Exception” propagation=”REQUIRED” />
Add <aop:config/> to applicationContext.xml file to apply the transaction advice using Spring AOP configuration

WEB-INF/applicationContext.xml

 

Service implementation Class

 

If we run the TestSpringTransaction.java we will get the same result for Annotation and XML based configurations.

Output

 

Thanks for your reading. Please leave a comment if you any query.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

One thought on “Declarative Transaction Management Example in Spring

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.