简体   繁体   English

事务暂停如何在Spring中运行?

[英]How does transaction suspension work in Spring?

My question is basically the same as is here , but I'm not satisfied with the answer so I'm writing this question. 我的问题与基本相同,但我对答案不满意所以我正在写这个问题。

In Spring Framework manual it is stated that for a PROPAGATION_REQUIRES_NEW the current transaction will be suspended. 在Spring Framework手册中,声明对于PROPAGATION_REQUIRES_NEW,当前事务将被暂停。 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. 这意味着您不能只是“不使用”原始事务并启动新事务 - 在启动新事务之前,您必须提交或回滚原始事务。

Example: 例:

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? 这样我们就可以停止使用原始连接并创建一个可以开始新事务的新连接?

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). 我在这里遗漏了一些显而易见的事情,没有人愿意解释它(至少在Spring文档,Spring in Action,Spring持久化Hibernate中)。

Thanks a lot! 非常感谢!

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. 没有连接级API来支持挂起事务,因此必须使用不同的连接来完成。 If you are using JTA with Spring, this is done by the JTA transaction manager. 如果您在Spring中使用JTA,则由JTA事务管理器完成。 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. 如果您正在使用DataSourceTransactionManager,您可以查看代码并查看它将作为“暂停资源”保存当前连接,并从新事务的数据源中获取新连接。

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

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