@Transactional REQUIRED vs REQUIRES_NEW example in spring boot

In this tutorial, we will see @Transactional REQUIRED vs REQUIRES_NEW example in spring boot and also will discuss what is the difference between Propagation.REQUIRED and Propagation.REQUIRES_NEW, when we should use Propagation.REQUIRED and when we can go for Propagation.REQUIRES_NEW. Let’s see brief about these attribute.

 

Understanding @Transactional REQUIRED vs REQUIRES_NEW.

@Transactional(propagation=Propagation.REQUIRED) – If there is not an existing transaction it will create a new transaction. In case if there is already an existing transaction it will not create a new transaction.

@Transactional(propagation=Propagation.REQUIRES_NEW) – Even if there is already an existing transaction it will create a new transaction i.e it will always create a new transaction.

To understand Propagation.REQUIRED  and Propagation.REQUIRES_NEW just assume following scenario. Suppose we want to save two entity Book.java and Stroy.java(both are independent entity means they don’t have either inheritance relationship or association relationship). We have two separate methods to save the book and story entity. The method which saving story is getting called from saveBook() method. Is it confusing? Let’s see in below code snippet.

We have two separate implementations classes BookServiceImpl.java and StoryServiceImpl.java with saveBook() and saveStory() method respectively. saveStory() is getting called from saveBook().

BookServiceImpl.java

StoryServiceImpl.java

 

Did you notice we are using propagation = Propagation.REQUIRED with saveBook() method and propagation = Propagation.REQUIRES_NEW with saveStory() method and calling saveStory() from saveBook() method(We are doing intentionally we can call saveStory() separately). The above code will run fine and we will have one entry for book and story in the database if we insert some book record from the postman(later we will see in the example).

When we say propagation = Propagation.REQUIRED it will check is any transaction is there or not. As of now for saveBook() we don’t have any transaction. It will create a new transaction for saveBook(). So what will happen if we say propagation = Propagation.REQUIRES_NEW, even we have an existing transaction it will create a new transaction(for example in this case saveStory() considered as a separate transaction).

Again question comes why we are doing these stuff? what benefit we will get?

Let’s modify BookServiceImple.java something like below –

In the above example, we are intentionally throwing RuntimeException, since we have an exception in our code for this transaction i.e saveBook() method(something wrong happened), spring will not save the book record in the database. Now, what about the Story entity? It will save in Database or not? 

Yes, we will have a record for Story entity in the database. Why? Don’t forget we are using propagation = Propagation.REQUIRES_NEW with saveStory() which means we have a new transaction for saveStory() which doesn’t depends on previous one i.e saveBook(). Hope it make sense. Let’s see an example for both cases.

 

@Transactional REQUIRED vs REQUIRES_NEW example in spring boot and Oracle.

  • 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 REQUIRED vs REQUIRES_NEW example in spring boot

Step 5 – Define the following classes and interfaces.

Book.java

Story.java

 

BookService.java – interface

StoryService.java – interface

BookRepository.java – interface

 

StoryRepository.java – interface

BookServiceImpl.java

StoryServiceImpl.java

BookController.java

 

JpaConfig.java

 

SpringMainExample.java

 

Testing using postman.

We are done now. Let’s run the application and test using postman.

 

 

Let’s check the database.

We have the book as well as story 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 in saveBook() and saveStory() logic is fine? Let’s see.

Let’s modify BookServiceImpl.java something like below.

Let’s run the application once again.

@Transactional REQUIRED vs REQUIRES_NEW example in spring boot

 

We have an exception in response which is expected behavior. In this case book entity will not save in the database but the story will save in the database.

 

@Transactional REQUIRED vs REQUIRES_NEW example in spring boot

 

That’s all about @Transactional REQUIRED vs REQUIRES_NEW example in spring boot. If you have any doubt or feel any difficulty to run the code, leave a comment.

You may like.

 

Spring Transaction Management Docs.

 

Top