需要Spring事务与REQUIRES_NEW:回滚事务 [英] Spring transaction REQUIRED vs REQUIRES_NEW : Rollback Transaction

查看:337
本文介绍了需要Spring事务与REQUIRES_NEW:回滚事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有 propagation = Propagation.REQUIRES_NEW 事务属性的方法:

  @Transactional(propagation = Propagation.REQUIRES_NEW)
public void createUser(final UserBean userBean){
//这里有些逻辑需要在DB
中修改}






此方法可以同时多次调用,并且对于每个事务if (独立于其他事务)发生错误。



问题是,这可能会强制Spring创建多个事务,即使另一个事务可用,并可能导致一些性能问题。




的传播速度= Propagation.REQUIRED 说:支持当前交易,如果不存在,则创建一个新交易。



解决性能问题,不是吗?



回滚我怎么样ssue?如果在使用现有事务时新方法调用回滚会怎么样?不会回滚整个交易甚至是以前的电话吗?




我想我的问题并不清楚足够:

我们有数百个客户端连接到我们的服务器。

对于每个客户端,我们自然需要发送有关事务的反馈(OK或异常 - >回滚)。



我的问题是:如果我使用 REQUIRED ,是否表示只使用一个事务,如果第100个客户端遇到问题时,第一个客户端的事务将会回滚?使用 REQUIRES_NEW 解析方案 $ c>只在从事务上下文中调用该方法时才有用;当方法从非事务性上下文中调用时,它的行为与 REQUIRED 完全一样 - 它会创建一个新的事务。

这并不意味着您的所有客户端只会有一个单一事务 - 每个客户端都将从非事务性上下文开始,只要请求处理将碰到 @Transactional ,它将创建一个新的事务。所以,考虑到这一点,如果使用 REQUIRES_NEW 对于该操作的语义有意义 - 比我想不担心性能 - 这会导致教科书不成熟的优化 - 我宁愿强调正确性和数据完整性,一旦收集了性能指标,而不是之前就担心性能。

在回滚 - 使用 REQUIRES_NEW 会强制新事务的开始,因此异常会回滚该事务。如果还有另一个正在执行的事务 - 根据操作的具体情况,将根据异常是否冒出堆栈或被捕获 - 根据您的选择进行回滚或不回滚。
另外,为了更深入地讨论事务策略和回滚,我建议:«交易策略:了解交易陷阱»,Mark Richards

I have a method that has the propagation = Propagation.REQUIRES_NEW transactional property:

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void createUser(final UserBean userBean) {
    //Some logic here that requires modification in DB
}


This method can be called multiple times simultaneously, and for every transaction if an error occurs than it's rolled back (independently from the other transactions).

The problem is that this might force Spring to create multiple transactions, even if another one is available, and may cause some performance problems.


Java doc of propagation = Propagation.REQUIRED says: Support a current transaction, create a new one if none exists.

This seems to solve the performance problem, doesn't it?

What about the rollback issue ? What if a new method call rolls back while using an existing transaction ? won't that rollback the whole transaction even the previous calls ?

[EDIT] I guess my question wasn't clear enough:

We have hundreds of clients connected to our server.

For each client we naturally need to send a feedback about the transaction (OK or exception -> rollback).

My question is: if I use REQUIRED, does it mean only one transaction is used, and if the 100th client encounters a problem the 1st client's transaction will rollback as well ?

解决方案

Using REQUIRES_NEW is only relevant when the method is invoked from a transactional context; when the method is invoked from a non-transactional context, it will behave exactly as REQUIRED - it will create a new transaction.

That does not mean that there will only be one single transaction for all your clients - each client will start from a non-transactional context, and as soon as the the request processing will hit a @Transactional, it will create a new transaction.

So, with that in mind, if using REQUIRES_NEW makes sense for the semantics of that operation - than I wouldn't worry about performance - this would textbook premature optimization - I would rather stress correctness and data integrity and worry about performance once performance metrics have been collected, and not before.

On rollback - using REQUIRES_NEW will force the start of a new transaction, and so an exception will rollback that transaction. If there is also another transaction that was executing as well - that will or will not be rolled back depending on if the exception bubbles up the stack or is caught - your choice, based on the specifics of the operations. Also, for a more in-depth discussion on transactional strategies and rollback, I would recommend: «Transaction strategies: Understanding transaction pitfalls», Mark Richards.

这篇关于需要Spring事务与REQUIRES_NEW:回滚事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆