@Transactional noRollbackForClassName example using spring boot

In this post we are going to see about @Transactional(noRollbackForClassName = {“SomeException”}).

Basic points related to @Transactional noRollbackForClassName.

 

  • We can use noRollbackForClassName as an attribute with @Transnational annotation as follows.

  • We can define zero, one or more exception names (exceptions which must be a subclass of Throwable class), for these exceptions our transaction will not get rollbacked. Suppose we use @Transactional(noRollbackForClassName = {“NullpointerException”})  with any method and NullpointerException comes inside some where in method, our transaction will not get rollbacked.

 

@Transactional noRollbackForClassName example using spring boot and Oracle.

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 noRollbackForClassName example using spring boot

Step 5 – Define the following classes and interfaces.

Book.java

 

BookService.java – interface

BookRepository.java – interface

 

BookServiceImpl.java

 

BookController.java

 

JpaConfig.java

 

SpringMainExample.java

And finally, we have application.properties

 

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

Testing Of Example using Postman.

Test from the postman with below request data.

 

Let’s check the DB.

@Transactional noRollbackForClassName example using spring boot

Even we have NullPointerException in our code, we have one record in the database. If you remove noRollbackForClassName = {“NullpointerException”}) from @Transactional annotation, it will not save any record, our transaction will get rollbacked.

That’s all about @Transactional noRollbackForClassName example using spring boot and Oracle.

You may like.

Spring Data JPA Examples.

Spring Transaction Management Docs.

 

Top