【问题标题】:Executing query in afterTransactionBegin method of Hibernate interceptor在 Hibernate 拦截器的 afterTransactionBegin 方法中执行查询
【发布时间】:2015-07-07 11:10:16
【问题描述】:

我添加了一个Hibernate Interceptor 并覆盖了afterTransactionBegin 方法,我正在尝试在此方法中执行数据库查询。

 public void afterTransactionBegin(Transaction tx) {
    LOG.info("Transaction begin: {}", applicationContext.getBean(ITxnDao.class).getCurrentTransactionId().toString());
}

但我得到了

Caused by: org.hibernate.HibernateException: No Session found for current thread
 at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)
 at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1039)

这意味着transaction实际上还没有开始。有什么方法可以实现吗?

【问题讨论】:

    标签: java spring hibernate transactions interceptor


    【解决方案1】:

    在调用afterTransactionBeginInterceptor 方法时事务已经启动。在您的情况下,您有一个与 Spring 相关的问题。

    确保您从使用 @Transactional 注释的 Service 方法调用 DAO 逻辑。

    【讨论】:

      猜你喜欢
      • 2020-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-26
      • 1970-01-01
      • 2021-10-05
      • 2023-03-31
      • 1970-01-01
      相关资源
      最近更新 更多