首页 > 解决方案 > org.hibernate.HibernateException:迁移到 Hibernate 5 后当前事务不在进行中

问题描述

我有一个类文件,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课堂上:</p>

我抛出异常

throw new Exception();

然后这将捕获CBRTask.java,然后导致数据库调用命中错误。

假设它不应该发生。因为这段代码在 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 类中手动抛出异常,并在同一个地方捕获,问题就不会发生。

标签: javahibernateexceptiontransactionalhibernate-session

解决方案


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

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

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


推荐阅读