@Transactional rollbackFor example using spring boot

In this post we will see @Transactional rollbackFor example using spring boot. We will also see what is the effect of using rollbackFor, how to use in real time scenario.

Basic points related to @Transactional rollbackFor attribute.

  • We can use rollbackFor attribute as a parameter with @Transnational annotation as follows.

  • The rollbackFor attribute tells to spring, hey spring please rollback the transaction for the given exception(the exceptions which we have provided as the value of rollbackFor).
  • If we don’t provide any value for rollbackFor or we don’t mention rollbackFor as a parameter with @Transnational annotation, spring provides default rollback for RuntimeException/unchecked exception and Error. Let’s see default values for rollbackFor.

What you write –

What spring understand-

In fact, it adds some more attributes and corresponding values, but here we are going to talk about rollbackFor only.

  • Spring doesn’t provide default rollback for Checked exception and its child exception(for example Exception or any custom exception which extends Exception class).
  • We can have multiple Exception class values for rollbackFor.

 

Let’s see some sample code snippet which will behave the same.

 

First One –

 

Second One –

 

Note – Observe the difference in above code snippets.

@Transactional rollbackFor example using spring boot.

Prerequisites –

  • JDK 1.8
  • Oracle 10g
  • Eclipse
  • maven
  • postman

 

Create maven project, Don’t forget to check ‘Create a simple project (skip)’click on next. Fill all details(GroupId – springtransactionexample, ArtifactId – springtransactionexample and name – springtransactionexample) and click on finish. Keep packaging as the jar.

Modify the pom.xml with below code.

 

Create the below directory structure.

@Transactional rollbackFor example using spring boot

Define the following classes and interfaces.

Book.java

 

BookService.java – interface

BookRepository.java – interface

 

BookServiceImpl.java

 

BookController.java

 

JpaConfig.java

 

SpringMainExample.java

 

We are good now. Let’s run the application.

 

Testing of example using postman.

 

Let’s check the DB.

@Transactional rollbackFor example using spring boot

We have the book record in the database. Yes, we don’t have any problem in code, no exception occurs, so the record is inserted in database and transaction completed. What will happen if we have some problem with saveBook()? Suppose we have some logic which throws NullpointerException after saving the book. Let’s modify the BookServiceImpl.java something like below.

 

Test from the postman.

 

@Transactional rollbackFor example using spring boot

 

Think! In DB we will have a record for book or not? The answer is no. Since we are using @Transactional annotation and rollbackFor = {RuntimeException.class}, our transaction will rollback because we have NullpointerException in saveBook () method. Again one question may come in mind, we have defined RuntimeException.class for rollbackFor, why the transaction is getting rollback even for NullpointerException? Don’t forget NullpointerException is a child of RuntimeException. Spring will take care transaction rollback for RuntimeExeception as well as its all child exception.

Again if we don’t provide rollbackFor = {RuntimeException.class} with @Transactional annotation what will happen? We have already discussed by default we will have  rollbackFor = {RuntimeException.class}, spring will take care.

 

Real-time use of rollbackFor.

So far we have learned, Spring provides a default value for rollbackFor i.e RuntimeException and Error. Even we don’t use rollbackFor attribute our transaction will be rollbacked for any RuntimeException.

So what is the use of rollbackFor ? Don’t forget spring is taking care of the only RuntimeException, not for Checked Exception. What will happen is some checked exception comes in our application.

If we do not use rollbackFor attribute for the checked exception, the transaction will not be rollbacked for the checked exception, which we may don’t want. If we want to rollback the transaction for all kind of exception including checked and unchecked we need to specify the rollbackFor attribute something like below.

rollbackFor = {Exception.class}

Now we are saying to spring, hey spring if you see any exception checked or unchecked please rollback the transaction(don’t save the record in DB).

Let’s see an example where we will use rolbackFor attribute with a value RuntimeException.class and will throw checked exception intentionally. Let’s see what happens.

Modify the classes and interface as below.

BookServiceImpl.java

 

BookService.java

 

BookController.java

 

If you run above changes and test from the postman, even we are using @Transactional annotation with rollbackFor = RuntimeException.class, we will have one book record in DB. Why? Because we are throwing checked exception in BookServiceImpl.java but we are saying to spring hey spring rollback the transaction for only RuntimeException/unchecked exception not for the checked exception.

@Transactional rollbackFor example using spring boot

 

Let’s check the DB.

Let’s modify the BookServiceImpl.java something like below.

rollbackFor = {Exception.class}

BookServiceImpl.java

 

test from the postman and see the DB we will not any record.

 

 

That is the expected result. We are telling to spring just rollback the transaction for any exception.

That’s all about @Transactional rollbackFor example using spring boot.

You may like –

 

Spring Transaction Management Docs.

 

Top