简体   繁体   中英

@Transactional applicability for read methods

I want to add Transactional support for my service class. If I add javax.transaction.Transactional annotation to Class which contains both read write methods how it behave?

By doing that for read methods in that class, does is make execution slow. Or does spring-boot ignore it for read methods?

For read operations, we use org.springframework.transaction.annotation.Transactional with parameter @Transactional(readOnly=true)

This just serves as a hint for the actual transaction subsystem; * it will not necessarily cause failure of write access attempts. and It will not slow execution.

  1. First thing you need make clear that the @Transactional in javax and Spring is different: https://stackoverflow.com/a/26387927/11827984

  2. The second thing is that scope of @Transactional . When you write in top of a method, its scope will apply for this method, when you write in top of a class, its scope will apply for this class. (More about here: https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/transaction.html#transaction-declarative-annotations ). And annotation has option for it @Transactional(readOnly=true) when you want for read only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM