1

What exactly happen when method is annotate with @Transactional ? Do @Transactional is called only when method contains database operations and what is the need of creating proxy

I have been asked this several times in interview but didn't get any clarification on reading answers from various blogs.

  • 2
    Does this answer your question? [Spring - @Transactional - What happens in background?](https://stackoverflow.com/questions/1099025/spring-transactional-what-happens-in-background) – Nora Na Nov 11 '21 at 10:01

2 Answers2

0

Transactional annotation signals Spring to create a proxy around the created bean. Spring uses AOP under the hood to delegate transaction-related activities to the underlying TransactionManager. AOP code is going to be invoked regardless you actually use DB access in your method or not. Having said that, there are issues regarding self-invocation of transactional methods, but this is the shortcoming of Spring Syle AOP at large.

I suggest you take a look at Spring proxies, in which cases a bean is proxied and when it is not, and also get a general feel for AOP since it is used heavily under the hood in Spring.

igobivo
  • 433
  • 1
  • 4
  • 17
0

@Transactional - When this annotation is declared at the class level, it applies as a default to all methods of the declaring class and its subclasses. Note that it does not apply to ancestor classes up the class hierarchy; inherited methods need to be locally redeclared in order to participate in a subclass-level annotation. For details on method visibility constraints, consult the Transaction Management section of the reference manual.

see

Vy Do
  • 46,709
  • 59
  • 215
  • 313