简体   繁体   English

@事务对读取方法的适用性

[英]@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? 如果我向包含两个读写方法的Class添加javax.transaction.Transactional批注,其行为如何?

By doing that for read methods in that class, does is make execution slow. 通过对该类中的read方法执行此操作,会使执行速度变慢。 Or does spring-boot ignore it for read methods? 还是spring-boot在读取方法时会忽略它?

For read operations, we use org.springframework.transaction.annotation.Transactional with parameter @Transactional(readOnly=true) 对于读取操作,我们将org.springframework.transaction.annotation.Transactional与参数@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 首先,您需要弄清楚javax和Spring中的@Transactional是不同的: https : //stackoverflow.com/a/26387927/11827984

  2. The second thing is that scope of @Transactional . 第二件事是@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 ). (有关更多信息,请参见: 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. 当您想只读时,注释具有@Transactional(readOnly = true)选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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