Spring 中的事务暂停是如何工作的? [英] How does transaction suspension work in Spring?

查看:17
本文介绍了Spring 中的事务暂停是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与这里基本相同,但是我对答案不满意所以我写了这个问题.

My question is basically the same as is here, but I'm not satisfied with the answer so I'm writing this question.

在 Spring Framework 手册中声明,对于 PROPAGATION_REQUIRES_NEW,当前事务将被暂停.这实际上是如何实施的?我知道大多数数据库不支持嵌套事务,并且只能在一个连接中运行一个事务.这意味着您不能只是不使用"原始事务并开始一个新事务 - 在开始新事务之前,您必须提交或回滚原始事务.

In Spring Framework manual it is stated that for a PROPAGATION_REQUIRES_NEW the current transaction will be suspended. How is this actually implemented? I know that most databases don't support nested transactions and can have only one transaction running in one connection. This means that you can't just "not use" original transaction and start a new one - before starting new one you must commit or rollback original transaction.

示例:

START TRANSACTION
SELECT ...
UPDATE ...
-- Now we run method with PROPAGATION_REQUIRES_NEW
-- How do we "suspend" transaction so we can start new one?
START TRANSACTION
UPDATE ...
COMMIT
-- We returned from the method, result was commited
-- Now we'd like to "unsuspend" the original transaction so it can be commited/rollbacked, but how?

或者这可能是使用另一个连接(会话对象)实现的吗?这样我们就可以停止使用原始连接并创建一个新的连接来开始新的事务?

Or is this possibly implemented using another connection (Session object)? So that we stop using the original connection and create a new one where we can start new transaction?

我在这里遗漏了一些如此明显的东西,以至于没人愿意解释它(至少在 Spring 文档、Spring in Action、Spring persistence with Hibernate 中没有).

I am missing here something so obvious that nobody cares to explain it (at least not in Spring docs, Spring in Action, Spring persistence with Hibernate).

非常感谢!

推荐答案

挂起事务的目的是将线程的当前事务更改为新事务.这与嵌套事务的语义不符,因为新事务和挂起事务是完全独立的.没有连接级 API 来支持挂起事务,因此必须使用不同的连接来完成.如果您在 Spring 中使用 JTA,则这由 JTA 事务管理器完成.如果您使用的是 DataSourceTransactionManager,您可以查看代码,看到它会将当前连接保存为挂起资源",并从数据源中为新事务获取新连接.

The point of suspending a transaction is to change the current transaction for a thread to a new one. This would NOT line up with the semantics of nested transactions because the new and suspended transactions are completely independent of each other. There is no connection-level API to support suspending transactions so this has to be done by using a different connection. If you are using JTA with Spring, this is done by the JTA transaction manager. If you are using DataSourceTransactionManager, you can look in the code and see that it will be saving off the current connection as a "suspended resource" and grabbing a new connection from the data source for the new transaction.

这篇关于Spring 中的事务暂停是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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