@Transactional readonly true example in spring boot

In this post, we will see @Transactional(readOnly = true) example using spring boot.

Points related to @Transactional(readOnly = true/false).

  • If we don’t provide any value for readOnly in @Transactional, then the default value will be false.

What we write –

What Spring understands –

 

  • Generally, we use @Transactional(readOnly = true) for search or retrieval operation to make sure we can only perform the read-only operation.

 

  • We can override readOnly  behaviour using @Modifying  annotation. For example, suppose @Transactional annotation has been used with class level or interface level as below and we want to override readOnly behaviour for one method(we don’t want to apply readOnly true for deleteOldBooks() method).

 

In the above example, we will able to delete record since we are overriding @Transactional(readOnly = true) nature using @Modifying annotation.

 

  • If we use @Transactional(readOnly = true) to a method which is performing create or update operation then we will not have newly created or updated record into the database but we will have the response data.

Below code will not give any exception but using @Transactional(readOnly = true) while create or update operation doesn’t make any sense. Right!

 

An example for @Transactional(readOnly = false) and @Transactional(readOnly = true).

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 readonly true example in spring boot

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.

 

@Transactional readonly true example in spring boot

Let’s check the DB.

@Transactional readonly true example in spring boot

Yes, we have one record in the database since we have mentioned in BookServiceImpl.java @Transactional(readOnly = false), we can perform read/write operation.

Note – Even we write @Transactional only with saveBook() method, it will behave the same because by default we have readOnly = false.

So what will happen if we do something like @Transactional(readOnly = true)? Let’s modify BookServiceImpl.java as below.

 

Let’s rerun the application and test from the postman. We have the same response as earlier but Does any record persist in DB? No there will not any record in DB. Since we are using @Transactional(readOnly = true), means we can perform the read-only operation, we can’t save any record in DB.

Let’s see the DB.

 

Note – we are using spring.jpa.hibernate.ddl-auto =create. Every time whenever we will rerun the application, we will have a new table.

That’s all about @Transactional readonly true example in spring boot.

Other examples related to Spring Transaction management.

Spring Data JPA Examples.

 

Spring Transaction Management Docs.

Top