简体   繁体   English

春季交易,无回滚

[英]Spring Transactional without rollback

 @Transactional
 public void setSomething(String name) { ... }

Sorry to ask this very basic question, Spring transactional annotation is so powerful but yet super hard to understand. 很抱歉问这个非常基本的问题,Spring事务处理注释功能强大,但是却很难理解。 Based on the code above, I don't have rollbackFor control, meaning, if there is exception, this transactional context will not be rollback. 根据上面的代码,我没有rollbackFor控件,这意味着,如果有异常,则此事务上下文将不会回滚。 But based on my experience in old way to covering transaction block, if there is no rollback for exception, commit will be skipped and cause the (Oracle) database's table being locked (suspend, other user can't commit their SQL). 但是根据我在处理事务块的旧方法上的经验,如果没有例外的回滚,则commit将被跳过,并导致(Oracle)数据库的表被锁定(挂起,其他用户无法提交其SQL)。 Will Spring have the same issue without using rollbackFor ? 如果不使用rollbackFor Spring是否会遇到相同的问题?

The default /@Transactional settings are: 默认的/ @ Transactional设置为:

  • Propagation setting is REQUIRED. 传播设置是必需的。
  • Isolation level is DEFAULT. 隔离级别为DEFAULT。
  • Transaction is read/write. 事务是读/写。
  • Transaction timeout defaults to the default timeout of the underlying 事务超时默认为底层服务器的默认超时
  • transaction system, or none if timeouts are not supported. 交易系统,如果不支持超时则不设置。
  • Any RuntimeException triggers rollback, and any checked Exception does not. 任何RuntimeException都会触发回滚,而任何选中的Exception都不会触发。

So in your case this will be rollbacked if you will have a RuntimeException. 因此,在您的情况下,如果您有RuntimeException,则会将其回滚。

But usually it is not sufficient to tell you simply to annotate your classes with the @Transactional annotation, add @EnableTransactionManagement to your configuration. 但是通常仅告诉您仅使用@Transactional注释对类进行注释,并在配置中添加@EnableTransactionManagement还是不够的。

You can configure exactly which Exception types mark a transaction for rollback, including checked exceptions. 您可以准确配置哪些异常类型将事务标记为回滚,包括已检查的异常。 The following XML snippet demonstrates how you configure rollback for a checked, application-specific Exception type. 以下XML片段演示了如何为已检查的,特定于应用程序的异常类型配置回滚。

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" rollback-for="NoProductInStockException"/>
<tx:method name="*"/>
</tx:attributes>

Or with annotations 或带有注释

@Transactional(rollbackFor=NoProductInStockException.class)

Detailed documentation you can find here: http://docs.spring.io/autorepo/docs/spring/4.2.x/spring-framework-reference/html/transaction.html 您可以在这里找到详细的文档: http : //docs.spring.io/autorepo/docs/spring/4.2.x/spring-framework-reference/html/transaction.html

Hope that helps. 希望能有所帮助。

The rollbackFor and related parameters are for fine-tuning. rollbackFor和相关参数用于微调。 If you omit them, the default behaviour is to rollback for RuntimeExceptions . 如果省略它们,则默认行为是回滚RuntimeExceptions Other exceptions don't perform a rollback and any changes made into the database will be committed. 其他异常不会执行回滚,对数据库所做的任何更改都将被提交。

The transaction is either committed or rolled back, there's no way a @Transactional method will result in an unterminated transaction (at least in normal operation). 该交易提交或回滚,没有办法一个@Transactional方法将导致未终止的交易(至少在正常操作)。

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

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