@Transactional timeout Using Spring Boot

In this post, we will see about @Transactional timeout Using Spring Boot and Oracle. For any transaction, we can provide a transaction timeout using @Transactinal annotation.

When we define @Transactional(timeout = 100) then we are saying our transaction should complete in given time frame otherwise we will get TransactionException(transaction time expired error).

The time value type should be int and it would be considered in milliseconds.

By default value of timeout is -1. That means no timeouts has been supported.

Let’s consider below code snippet.

Let’s see what we are doing in the above code snippet.

We are retrieving all student records using the CrudRepository findAll() method. Suppose we one thousand students record in the database and we want to fetch all records using findAll() method. Assume it takes 50 milliseconds to fetch one thousand students. Since we have provided 10o as the value of timeout within @Transactional annotation, we are good it will fetch all students. But if it takes more than 100 milliseconds we will get transaction timeout expired error as below.

@Transactional timeout Using spring Boot

Let’s see an example of @Transactional timeout Using spring Boot and Oracle.

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

Modify pom.xml

 

Note – In pom.xml we have defined javac.exe path in configuration tag. You need to change accordingly i.e where you have installed JDK.

If you see any error for oracle dependency then follow these steps.

Directory structure –

@Transactional timeout Using spring Boot

 

Let’s see classes for @Transactional timeout Using Spring Boot.

Student.java

StudentController.java

 

Note – See more details about @Controller and RestController here.

StudentRepository.java – interface

 

SpringMain.java

Note – See more details about @ComponentScan here.

JpaConfig.java

Note – See more details about @Configuration annotations here.

application.properties

Let’s run the SpringMain class(run as java application).

Perform saveall operation first using below REST API.

http://localhost:9091/student/saveall

Request Data.

 

Response data –

 

 

Perform getall operation using below REST API.

http://localhost:9091/student/getall

Since we have defined 1 millisecond as transaction timeout(@Transactinal timeout = 1) in StudentServiceImpl.java mostly we will get transaction timeout expired error.

Increase the timeout value something 10ms, 20ms or 100 ms(or any bigger number depends on system configuration). I increase one ms to ten ms and we should not have any error.

Note – Transaction timeout expired error depends on machine configuration.

That’s all about @Transactional timeout example in spring Boot.

you may like.

Spring Data JPA Examples Using Spring Boot and Oracle.

Spring Transactional docs.

Top