简体   繁体   English

确保调用的方法在 Spring @Transactional 边界内

[英]Ensure methods called are within Spring @Transactional boundary

I'm using org.springframework.transaction.annotation.Transactional and am calling methods that I only want to be able to be called from a method annotated with @Transactional .我正在使用org.springframework.transaction.annotation.Transactional并且正在调用我只想能够从用@Transactional注释的方法调用的方法。

For reasons I won't pollute this question with, the methods I want to call as part of the transaction should never be called out with the transaction boundary and I want to enforce this.出于我不会污染这个问题的原因,我想作为事务的一部分调用的方法不应该被事务边界调用,我想强制执行这一点。

Is there an annotation or similar I can use to ensure these methods are only able to be successfully called from a method annotated with @Transactional ?是否有注释或类似的我可以用来确保这些方法只能从用@Transactional注释的方法成功调用?

In a simplest case you can use Propagation.MANDATORY (ie @Transactional(propagation=Propagation.MANDATORY) ) transactional propagation instead of default Propagation.REQUIRED .在最简单的情况下,您可以使用Propagation.MANDATORY (即@Transactional(propagation=Propagation.MANDATORY) )事务传播而不是默认的Propagation.REQUIRED

If you use programmatic transactions then you check TransactionStatus getters - even if your propagation is REQUIRED you can verify that TransactionStatus.isNewTransaction() returns false.如果您使用程序化事务,那么您检查TransactionStatus getter - 即使您的传播是REQUIRED您也可以验证TransactionStatus.isNewTransaction()返回 false。

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

相关问题 Spring中的事务处理方法 - Transactional methods in Spring Spring 中 @Async 方法上的 @Transactional - @Transactional on @Async methods in Spring 带有 Spring Boot 和事务边界的 graphql-spqr - graphql-spqr with Spring Boot and Transactional Boundary 如果在内部@Spring注释了方法,则Spring @Transactional注释方法是否会覆盖@Transactional(readOnly = true)方法? - Does a Spring @Transactional annotated method overwrite an @Transactional(readOnly=true) method if called within it? 线程内的Spring @Transactional支持 - Spring @Transactional support within a thread 从服务类中调用时,Spring @Transactional不适用于带注释的方法 - Spring @Transactional doesn't work for an annotated method when called from within service class Spring 为在 @Transactional 注释方法中调用的每个 JpaRepository 方法打开一个新事务 - Spring opens a new transaction for each JpaRepository method that is called within an @Transactional annotated method 如何在 Spring 中为事务方法编写事务单元测试? - How to write Transactional Unit Tests for Transactional Methods in Spring? Spring和Hibernate:非事务性服务方法 - Spring & Hibernate: non transactional service methods 在单个事务中运行spring @Transactional注释方法 - Run spring @Transactional annotated methods in a single transaction
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM