Spring Boot Transaction Management Internal Working

Transaction Management encompasses the supervision and handling of errors and processes that may occur during a transaction.
@Transactional
Spring managing transactions have provided annotation. This annotation can be defined on classes and methods. In Spring Boot, transaction management does not require a configuration. But in Spring, It requires a configuration.
@Transactional(propagation = Propagation.REQUIRED)

Deep Look
AOP (Aspect Oriented Programming)
In Spring, transaction management is facilitated through Aspect-Oriented Programming (AOP). AOP effectively separates cross-cutting concerns from the core business logic. In a typical n-layer architecture, which includes the presentation layer (API layer), business layer (service layer), and data access layer (repository layer), cross-cutting concerns are usually defined within the business layer. These concerns encompass aspects such as security, validation, transaction management, logging, performance optimization, and other related processes. This truly enhances the clarity of the code.
When request is sent to the server-side payment method, which was already declared as a transactional method, interceptors come into play and process either before or after the business logic for transactional annotation using the proxy.
Proxy
Spring uses dynamic proxies, which means it creates proxies at runtime. These proxies are generated for objects marked with the @Transactional annotation. When a method with this annotation is called, it interrupts the normal flow and allows the processing of additional business logic either before or after the main logic.
The Proxy controls access to the original object, enabling processing either before or after the main object.
Example of Proxy
Typically, the application includes one service named CategoryService, which has a create method. CategoryServiceImpl and the Proxy that is created by spring in runtime extend CategoryService. This service is wrapped with a proxy, which means the proxy manages access to the real object and implements some business logic either before or after, such as rollback or commit transaction.
Spring creates these proxies in runtime. What if the application does not have an interface? If the application lacks an interface, the solution is straightforward. The Application Context extends a proxy directly from the concrete class. For example, if the application includes only a concrete class like CategoryService, the proxy simply extends CategoryService.


        public interface CategoryService {            void create();         }
import org.springframework.transaction.annotation.Transactional;public class CategoryServiceImpl implements CategoryService {

public CategoryServiceImpl(){

}

@Override
@Transactional
public void create() {
// CREATE CATEGORY
System.out.println("Category Service");
}
}
public class ProxyCategoryService implements CategoryService {

private CategoryServiceImpl categoryService;

public ProxyCategoryService(){
System.out.println("Proxy Category Service");
}



@Override
public void create() {
if (categoryService == null){
categoryService = new CategoryServiceImpl();
}

try {
System.out.println("Get Transaction");
categoryService.create();
throw new RuntimeException();
}catch (Exception e){
System.out.println("Rollback Transaction");
}

System.out.println("Commit Transaction");
}
}
public class Test {
public static void main(String[] args) {
CategoryService categoryService = new ProxyCategoryService();
categoryService.create();
}
}

As you can see in this code, we can provide some business logic such as commit, rollback or more either before or after categoryService.create() function.

Post a Comment

Previous Post Next Post