@Transactional annotation in Spring Boot

Nikhil Sukhani
3 min readDec 27, 2022

--

meme

The @Transactional annotation is used to mark a method or a class as transactional, meaning that any database operations performed within the marked method or class will be executed within a transaction. If the transaction is successful, the changes will be committed to the database. If an error occurs and the transaction is rolled back, the changes will not be persisted in the database.

Here is an example of how the @Transactional annotation can be used on a method:

@Transactional
public void updateUser(User user) {
// Perform database update here
}

And here is an example of how the @Transactional annotation can be used on a class:

@Transactional
public class UserService {
public void updateUser(User user) {
// Perform database update here
}
}

By default, the @Transactional annotation will create a new transaction for the marked method or class if one does not already exist. If a transaction does already exist, the marked method or class will be executed within the existing transaction.

You can also specify the propagation behaviour of a transaction by using the propagation attribute of the @Transactional annotation. For example, you can specify that a transaction should always be created, regardless of whether a transaction already exists, by using the PROPAGATION_REQUIRED attribute:

@Transactional(propagation = Propagation.REQUIRED)
public void updateUser(User user) {
// Perform database update here
}

You can also specify the isolation level of a transaction by using the isolation attribute. The isolation level determines how much a transaction is isolated from other transactions. For example, you can use the ISOLATION_SERIALIZABLE attribute to specify that the transaction should be completely isolated from other transactions:

@Transactional(isolation = Isolation.SERIALIZABLE)
public void updateUser(User user) {
// Perform database update here
}

You can also specify the maximum time that a transaction should take to complete by using the timeout attribute. For example, you can use the following code to specify that a transaction should time out after 10 seconds:

@Transactional(timeout = 10)
public void updateUser(User user) {
// Perform database update here
}

The @Transactional annotation also supports other attributes, such as readOnly, which can be used to indicate that a transaction should be read-only, and rollbackFor, which can be used to specify the exceptions that should cause a transaction to roll back. You can find more information about these attributes and others in the Spring documentation: Transactional (Spring Framework)

It’s important to note that the @Transactional annotation only works if you have enabled transactions in your application. In Spring Boot, you can enable transactions by adding the spring-boot-starter-jdbc or spring-boot-starter-data-jpa dependency to your project, depending on whether you are using JDBC or JPA for database access. You can then use the @EnableTransactionManagement annotation to enable transaction management in your application.

@Configuration
@EnableTransactionManagement
public class TransactionConfig {
// Configuration goes here
}

You can also specify the transaction manager to be used in your application by using the @Bean annotation to create a bean for the transaction manager. For example, if you are using JDBC for database access, you can use the following code to create a DataSourceTransactionManager bean:

@Bean
public DataSourceTransactionManager transactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}

If you are using JPA for database access, you can use the JpaTransactionManager class as your transaction manager.

@Bean
public JpaTransactionManager transactionManager(
EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}

Once you have enabled transactions and configured a transaction manager, you can use the @Transactional annotation to mark your methods and classes as transactional.

It’s also worth noting that the @Transactional annotation can be used in combination with AOP (Aspect-Oriented Programming) to apply transactions to methods or classes that are not directly annotated with @Transactional. This can be useful if you want to apply transactions to a large number of methods or classes without having to annotate each one individually.

In summary, the @Transactional annotation is a powerful tool for managing transactions in a Spring Boot application. It allows you to easily mark methods and classes as transactional, specify the propagation and isolation levels of transactions, and customize the behaviour of transactions using various attributes. By using transactions, you can ensure that your database updates are executed in a consistent and reliable way, and that any errors are properly handled and rolled back.

If this article helped you in any way, please react with a clap and help it reach a wider audience :)

--

--

Responses (6)