【问题标题】:How to run two Spring transactions in a single hibernate Session?如何在单个休眠会话中运行两个 Spring 事务?
【发布时间】:2018-09-14 15:25:26
【问题描述】:

我知道,有一种方法可以降低到低级别 - 获取连接并在单个休眠会话中手动执行两个事务。

但问题是 - 如何通过 @Transactional 注释在同一个 Session 中调用第二个嵌套事务(不使用“低级黑客”或手写自定义事务管理)?

一些可能的代码:

@Service
public class DoubleTransaction {

@Autowired
private SessionFactory sf;

@Autowired
private NestedTeHandler nestedHandler;

@Transactional
void invokeTransaction() {
    Session cs = sf.getCurrentSession();
    Session nestedCs = nestedHandler.invokeNested(sf);
    System.out.println(cs == nestedCs);
}}

@Service
public class NestedTeHandler {

@Transactional
Session invokeNested(SessionFactory sf) {
    return sf.getCurrentSession();
}}

【问题讨论】:

  • 你可以使用事务管理

标签: java spring hibernate jta


【解决方案1】:

你也许可以这样做

@org.springframework.transaction.annotation.Transactional(propagation = Propagation.NESTED)

NestedTeHandler.invokeNested。请参阅https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/annotation/Transactional.html 的文档。

另请参阅此问题:Differences between requires_new and nested propagation in Spring transactions

【讨论】:

  • 您能否提供一些确认,嵌套事务不会打开新会话?
  • 这就是文档所暗示的。我个人从未使用过该功能,因此无法确认。
猜你喜欢
  • 1970-01-01
  • 2012-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-06
  • 2017-06-05
  • 1970-01-01
相关资源
最近更新 更多