交易的传播行为

发布于 2024-10-30 00:47:20 字数 538 浏览 1 评论 0原文

我正在为 spring aop 使用基于注释的声明性方法。 示例代码

ClassA{
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
add()
{
method1();
method2();
method3();

}
}

但我仍然​​对使用propagation.does propagation.Requires_New表示怀疑,意味着每个请求都会启动新事务。

第二个问题:

method2、method3等方法失败是否会导致事务回滚?

如果有人能帮助我学习交易传播,我将非常高兴。

有人可以给我提供一个现实世界的例子,我们需要参与现有的交易。因为我想象我们在上面的例子中使用的添加函数将独立于所有用户,或者任何其他函数将独立于每个调用的用户。我无法找到使用其他传播行为(如 PROPAGATION_SUPPORTS 、PROPAGATION_MANDATORY、PROPAGATION_REQUIRES_NEW 等)的示例

I am using annotation based declarative approach for spring aop.
sample code

ClassA{
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
add()
{
method1();
method2();
method3();

}
}

But I have still doubt on use of propagation.does propagation.Requires_New means that each request will start new transaction.

Second question:

Does failure of any method like method2,method3 will cause the transaction to rollback?

I will be very happy if any can help me to leans transaction propagation.

can someone provide me a real world example where we need a participate in existing transaction.because I visualise that add function that we are using in above example will be independent for all users,or any other function will be independent to each user who is calling. I am not able to find example where other propagation behaviour like PROPAGATION_SUPPORTS ,PROPAGATION_MANDATORY,PROPAGATION_REQUIRES_NEW etc. are used

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奶茶白久 2024-11-06 00:47:20

回答这个评论,而不是实际的问题:

交易是特定于会话的或
请求特定 – Vish 3 小时前

都不是。 request 和 session 都是特定于 Web 的范围,而 Spring Transaction 抽象与 Web 技术无关。

@Transactional 的范围是每个方法调用,因为 @Transactional 是通过 Spring AOP 实现的。事务状态保存在 ThreadLocal 变量中,这些变量在进入最外层的 @Transactional 方法时初始化,并在离开时通过提交或回滚清除。整个抽象在 Java 方法级别上工作,因此不需要 Web 容器或从 Web 容器中获益。


并在下面的评论中回答这个问题:

谢谢@sean,我仍然无法得到
回答其他传播在哪里
像 PROPAGATION_SUPPORTS 这样的行为
,PROPAGATION_MANDATORY,PROPAGATION_REQUIRES_NEW
等都被使用。请参考上面的
整个问题

这是 传播值与我的评论:

强制
支持当前事务,如果满足则抛出异常
不存在。

不启动新事务,仅检查事务是否处于活动状态(必须位于另一个 @Transactional 方法调用或以编程方式创建的事务内)

嵌套
如果当前事务在嵌套事务中执行
存在,表现得像
PROPAGATION_REQUIRED 否则。

如果事务存在,则启动嵌套事务,否则启动新事务。

从不
非事务性执行,如果有事务则抛出异常
存在。

不启动事务。如果存在事务则失败。

NOT_SUPPORTED
以非事务方式执行,如果有则暂停当前事务
存在。

不启动事务。暂停任何现有交易。

必需
支持当前交易,如果没有则创建新交易
存在。

如果存在交易,则使用该交易,如果不存在,则创建一个新交易。 在 95% 的情况下,这就是您所需要的。

REQUIRES_NEW
创建一个新事务,如果有则暂停当前事务
存在。

无论是否存在现有事务,始终创建新事务。如果有,它将在此方法执行期间暂停。

支持
支持当前交易,执行
如果不存在,则为非事务性的。

如果存在交易,则可以使用交易,但不需要交易(也不会启动新交易)。


在大多数情况下,REQUIRED 就是您所需要的(因此它是 REQUIRED 中的默认设置 ) code>@Transactional 注释)。我个人从未见过除了 REQUIREDREQUIRES_NEW 之外的任何其他值。

Answering this comment, not the actual question:

transaction are session specific or
request specific – Vish 3 hours ago

Neither. request and session are both web-specific scopes, while the Spring Transaction abstraction has nothing to do with web technologies.

The scope of @Transactional is per method invocation, as @Transactional is implemented through Spring AOP. The transactional state is kept in ThreadLocal variables which are initialized when the outermost @Transactional method is entered and cleared with commit or rollback when it is left. This whole abstraction works on Java method level, and hence does not require or profit from a web container.


And in response to this Question in the comment below:

thanks @sean,i am stil not able to get
answer where other propagation
behaviour like PROPAGATION_SUPPORTS
,PROPAGATION_MANDATORY,PROPAGATION_REQUIRES_NEW
etc are used. please refer above for
whole question

Here's the list of Propagation values with my comments:

MANDATORY
Support a current transaction, throw an exception if
none exists.

Does not start a new Transaction, just checks whether a transaction is active (must be inside either another @Transactional method call or a programmatically created transaction)

NESTED
Execute within a nested transaction if a current transaction
exists, behave like
PROPAGATION_REQUIRED else.

Start a nested transaction if a transaction exists, start a new transaction otherwise.

NEVER
Execute non-transactionally, throw an exception if a transaction
exists.

Does not start a transaction. Fails if a transaction is present.

NOT_SUPPORTED
Execute non-transactionally, suspend the current transaction if one
exists.

Does not start a transaction. Suspends any existing transaction.

REQUIRED
Support a current transaction, create a new one if none
exists.

If a transaction exists, use that, if not, create a new one. In 95% of cases, this is what you need.

REQUIRES_NEW
Create a new transaction, suspend the current transaction if one
exists.

Always creates a new transaction, no matter if an existing transaction is present. If there is, it will be suspended for the duration of this method execution.

SUPPORTS
Support a current transaction, execute
non-transactionally if none exists.

Can use a transaction if one is present, but doesn't need one (and won't start a new one either)


In most cases, REQUIRED is what you need (hence it's the default in the @Transactional annotation). I have personally never seen any other value but REQUIRED and REQUIRES_NEW in use.

耀眼的星火 2024-11-06 00:47:20

事务传播指示给定方法在调用时应该有什么行为。 REQUIRES_NEW 意味着即使有正在进行的事务,也应始终启动新事务。

例如,如果 method1() 定义了 REQUIRES_NEW,那么它将在新事务中执行。

异常会回滚当前活动事务,是的。

Transaction propagation indicates what should be the behaviour of the given method when it is invoked. REQUIRES_NEW means that a new transaction should always be started, even if there is an ongoing transaction.

If method1(), for example, defines REQUIRES_NEW, than it will execute in a new transaction.

An exception will rollback the current active transaction, yes.

郁金香雨 2024-11-06 00:47:20

是的。 Requires_New意味着每个请求都会开始新的事务。并且method2、method3中的Yes失败将导致事务回滚,具体取决于回滚属性。
检查事务属性。

Yes. Requires_New means that each request will start new transaction. and Yes failure in method2,method3 will cause the transaction to rollback, depending on the rollback properties.
check Transactional properties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文