【问题标题】:org.hibernate.HibernateException: Current transaction is not in progress after migrate to Hibernate 5org.hibernate.HibernateException:迁移到 Hibernate 5 后当前事务不在进行中
【发布时间】:2019-02-25 10:12:38
【问题描述】:

我有一个类文件,CBRTask.java

在这个CBRTask.java,我有一些代码如下:

@Autowired
private CbrService cbrService;

public CBRPayment execute() {

   try {
        cbrService.sendCR( this.entity );
        // If I manually throw Exception here, no problem in catch
   catch (Exception exception) {
   // do something to call db, but hit error
   }
}

然后在我的CbrService类中:

我抛出异常

throw new Exception();

那么这会catch in CBRTask.java,然后导致db调用命中错误。

假设它不应该发生。因为这段代码在 Hibernate 3 中运行良好。在我迁移到 hibernate 5 之后,然后才点击。

这是错误日志:

org.hibernate.HibernateException: Current transaction is not in progress
    at org.hibernate.context.internal.JTASessionContext.currentSession(JTASessionContext.java:81)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:721)

如果我在 CBRPayment 类中手动抛出异常,并在同一个地方捕获,问题就不会发生。

【问题讨论】:

  • 能不能也显示一下CBRTask这个类的内容?因为一般来说,此消息意味着您要检索没有可用事务的当前会话。这可能有多种原因(例如,从未启动过事务,已经回滚了......)。我想该服务的代码会有所帮助。我可以想到以下几点:也许您已经在服务中回滚事务,并且在您想在任务类上调用另一个 db 调用之后,您会收到上述错误。
  • 检查你的类CbrService是否有一个@Transaction注解,或者如果你喜欢你的sendCR(..)方法。如果注释已经存在,请在此失败之前搜索其他日志条目,如迈克尔所说。之前发生过一些事情。

标签: java hibernate exception transactional hibernate-session


【解决方案1】:

这实际上不是因为将 Hibernate 3 升级到 Hibernate 5。而是与事务管理器有关。在此之前,我使用的是WebSphereUowTransactionManager,它会在每次捕获异常后自动创建新事务。

当我来到 Jboss 环境时,我使用的是JTATransactionManager,不知道为什么即使有一个事务被捕获,这个管理器也会使用回现有事务。

我所做的是,将GlobalRollbackOnParticipationFailure 更改为 false。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多