Transaction Management in Spring Boot
Hello everyone,
Transaction management is one of the difficult features to understand for the developers and hence I decided to write on this topic. Let us take a look at how the Spring framework does transaction management.
There are 2 types of transaction management namely
- Declarative
- Programmatic
1. Declarative Transaction Management
Declarative transaction management is the most widely used method in which the developers tell the framework to handle the transactions. This can be achieved in 2 ways ie. through XML configurations or through annotations
Now, let us focus only on the annotation part.
We need to include 2 annotations for this to take effect
- @EnableTransactionManagement annotation is usually on the main class. Adding @EnableTransactionManagement annotation creates a PlatformTransactionManager for you — with its JDBC auto-configurations.
@SpringBootApplication
@EnableTransactionManagement
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2. @Transactional annotation on a service class or a method
Using Transactional annotation on the class level marks the class as transactional
@Transactional
public…