【发布时间】: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