【问题标题】:Spring + Hibernate migration - currentSession() method throwing exceptionSpring + Hibernate 迁移 - currentSession() 方法抛出异常
【发布时间】:2017-02-26 07:21:30
【问题描述】:

我已将 Spring 从 3.1 迁移到 4.1.3,将 Hibernate 3.2 迁移到 4.3.9

作为迁移的一部分,我在导入和代码下方进行了修改

旧导入

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

新导入

import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

和代码更改

在休眠 3 中我有以下代码

public Session getCurrentSession() {
    Session session = getSession();
    session.setFlushMode(FlushMode.MANUAL);
    return session;
}

现在我已经根据新的jars进行了如下修改

public Session getCurrentSession() {
    Session session = currentSession();
    session.setFlushMode(FlushMode.MANUAL);
    return session;
    }

经过上述更改后,我遇到了异常

    Could not obtain transaction-synchronized Session for current thread
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
    at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
    at org.springframework.orm.hibernate4.support.HibernateDaoSupport.currentSession(HibernateDaoSupport.java:129)

我没有在我的应用程序中使用注释

我无法解决问题。请帮助我了解异常的可能原因

【问题讨论】:

  • 修复您的交易设置。这个异常告诉你你没有(正确地)设置你的交易。
  • @M.Deinum 我在配置文件中添加了 标签,但仍然遇到同样的问题。我还需要添加什么吗?请确认
  • 只添加该标签而不添加@Transactional 几乎没用。
  • @Raj 如果您没有在项目中使用注释。那么,使用<tx:annotation-driven/> 没有任何好处。
  • @gschambial 是的,但我提供了一个线索,但没有成功:)

标签: spring hibernate spring-4 hibernate-4.x


【解决方案1】:

我猜你需要为你的sesssionFactory 添加transactionManager

<bean id="transactionManager"  class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory">
         <ref local="mySessionFactory"/>
    </property>
  </bean>

其中,mySessionFactory 是您的会话工厂 Bean ID。

正如您所说,您没有在项目中使用注释。然后,您必须使用AOP 在方法级别启用事务管理。

<tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
    <tx:method name="select*"  read-only="true" />
    <tx:method name="*" />
  </tx:attributes>
</tx:advice>
<aop:config>
  <aop:pointcut id="txPointcut" expression="execution(* example.MyClass.*(..))" />
  <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config>

像这样为您的方法添加事务支持。

如果您使用的是 MAVEN 或 gradle,请在您的 POM 中添加以下条目,或者您可以简单地下载 jar 并将其添加到您的类路径中。

<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>4.3.3.RELEASE</version>
</dependency>

【讨论】:

  • 确保您使用的是org.springframework.orm.hibernate4.HibernateTransactionManager 而不是org.springframework.orm.hibernate3.HibernateTransactionManager
  • 应用程序也没有使用 aop。如果你愿意,我可以发布我的 tx 详细信息
  • @Raj 添加事务 AOP 支持没有复杂的配置。我已经更新了添加 AOP 支持的答案。
  • @Raj 好运吗?
  • @gschambial 很抱歉回复晚了.. 还没有运气.. 有没有其他方法可以避免注释?作为当前应用程序不使用注释,我想继续不使用注释。还是 @Transactional 对于 spring4 是强制性的?
猜你喜欢
  • 2014-07-27
  • 1970-01-01
  • 2019-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-05
  • 1970-01-01
相关资源
最近更新 更多