简体   繁体   English

@Transactional(传播= Propagation.REQUIRED)

[英]@Transactional(propagation=Propagation.REQUIRED)

if some one can explain what this annotation do and when exactly we use it : 如果有人可以解释这个注释的作用以及我们何时使用它:

@Transactional(propagation=Propagation.REQUIRED)

Thanks 谢谢

If you need a laymans explanation of the use beyond that provided in the Spring Docs 如果您需要外人解释超出Spring Docs中提供的用法

Consider this code... 考虑这段代码......

class Service {
    @Transactional(propagation=Propagation.REQUIRED)
    public void doSomething() {
        // access a database using a DAO
    }
}

When doSomething() is called it knows it has to start a Transaction on the database before executing. 当doSomething()被调用时,它知道它必须在执行之前在数据库上启动一个Transaction。 If the caller of this method has already started a Transaction then this method will use that same physical Transaction on the current database connection. 如果此方法的调用者已经启动了Transaction,则此方法将在当前数据库连接上使用相同的物理 Transaction。

This @Transactional annotation provides a means of telling your code when it executes that it must have a Transaction. 这个@Transactional注释提供了一种在执行时告诉代码它必须具有事务的方法。 It will not run without one, so you can make this assumption in your code that you wont be left with incomplete data in your database, or have to clean something up if an exception occurs. 它不能在没有一个的情况下运行,因此您可以在代码中假设您不会在数据库中留下不完整的数据,或者如果发生异常则必须清理某些内容。

Transaction management is a fairly complicated subject so hopefully this simplified answer is helpful 交易管理是一个相当复杂的主题,所以希望这个简化的答案是有帮助的

When the propagation setting is PROPAGATION_REQUIRED, a logical transaction scope is created for each method upon which the setting is applied. 当传播设置为PROPAGATION_REQUIRED时,将为应用该设置的每个方法创建逻辑事务范围。 Each such logical transaction scope can determine rollback-only status individually, with an outer transaction scope being logically independent from the inner transaction scope. 每个这样的逻辑事务范围可以单独确定仅回滚状态,外部事务范围在逻辑上独立于内部事务范围。 Of course, in case of standard PROPAGATION_REQUIRED behavior, all these scopes will be mapped to the same physical transaction. 当然,在标准PROPAGATION_REQUIRED行为的情况下,所有这些范围将映射到同一物理事务。 So a rollback-only marker set in the inner transaction scope does affect the outer transaction's chance to actually commit (as you would expect it to). 因此,内部事务范围中的仅回滚标记集确实会影响外部事务实际提交的机会(正如您所期望的那样)。

在此输入图像描述

http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/transaction.html http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/transaction.html

In Spring applications, if you enable annotation based transaction support using <tx:annotation-driven/> and annotate any class/method with @Transactional(propagation=Propagation.REQUIRED) then Spring framework will start a transaction and executes the method and commits the transaction. 在Spring应用程序中,如果使用<tx:annotation-driven/>启用基于注释的事务支持并使用@Transactional(propagation = Propagation.REQUIRED)注释任何类/方法,那么Spring框架将启动一个事务并执行该方法并提交交易。 If any RuntimeException occurred then the transaction will be rolled back. 如果发生任何RuntimeException,则将回滚事务。

Actually propagation=Propagation.REQUIRED is default propagation level, you don't need to explicitly mentioned it. 实际传播= Propagation.REQUIRED是默认传播级别,您不需要明确提及它。

For further info : http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/transaction.html#transaction-declarative-annotations 有关详细信息,请访问: http//static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/transaction.html#transaction-declarative-annotations

To understand the various transactional settings and behaviours adopted for Transaction management, such as REQUIRED , ISOLATION etc. you'll have to understand the basics of transaction management itself. 要了解事务管理采用的各种事务设置和行为,例如REQUIREDISOLATION等,您必须了解事务管理本身的基础知识。

Read Trasaction management for more on explanation. 阅读Trasaction管理以获取更多解释。

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

相关问题 @Transactional(propagation = Propagation.REQUIRED)在春季? - @Transactional(propagation = Propagation.REQUIRED) in spring? @Transactional(readOnly = false,传播= Propagation.REQUIRED)引发异常 - @Transactional (readOnly = false, propagation = Propagation.REQUIRED) is throwing exception 春季:传播。要求不起作用 - Spring:Propagation.REQUIRED not working 只要没有错误就可以更新/插入数据库(@Transactional(propagation = Propagation.REQUIRED)) - Just update/insert into DB when there are no errors (@Transactional(propagation = Propagation.REQUIRED)) 如何从@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class) 中排除特定异常? - How To Exclude Specific Exception From @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)? Java Spring中的并发性:@Transactional(propagation = Propagation.REQUIRED,隔离= Isolation.SERIALIZABLE)不起作用 - Concurrency in java spring : @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE) not working @Transactional传播私人方法 - @Transactional propagation on private methods Spring @事务隔离传播 - Spring @Transactional Isolation propagation Spring @Transactional传播属性 - Spring @Transactional propagation property Spring @Transactional - 隔离、传播 - Spring @Transactional - isolation, propagation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM