Сообщения

Сообщения за октябрь, 2024

Entity Lifecycle

Изображение
  1. package com.appsdeveloperblog.ws.demo_transactional; import com.appsdeveloperblog.ws.demo_transactional.entity.Account; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; import lombok. RequiredArgsConstructor ; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure. SpringBootApplication ; @SpringBootApplication @RequiredArgsConstructor public class DemoTransactionalApplication implements CommandLineRunner { private final EntityManagerFactory emf ; public static void main (String[] args) { SpringApplication. run (DemoTransactionalApplication. class , args); } @Override public void run (String... args) throws Exception { EntityManager em = emf .createEntityManager(); em.getTransaction().begin(); // Transient mode: because it does not exist in DB Account account = new Account(); // Ma

ACID, Isolation level, @Transactional

Изображение
 1. In general, Spring does not create proxy classes by default for stereotype-annotated classes (such as those annotated with @Component , @Service , @Repository , etc.). Spring only creates a proxy for a class when it needs to apply some kind of aspect-oriented programming (AOP) , typically for features like transaction management, security, or custom-defined interceptors. Here are the conditions under which Spring creates a proxy: AOP Requirements : When there is a specific aspect (e.g., @Transactional , @Cacheable , or @Async ), Spring uses a proxy to implement that aspect without modifying the class itself. Interfaces : If a class implements an interface and Spring needs to create a proxy (e.g., for @Transactional ), Spring will use JDK dynamic proxies by default, which only proxy interfaces. For classes without an interface, Spring uses CGLIB to create a subclass proxy. In the example you provided, B is annotated with @Component , but it doesn’t have any aspect requirements. So