【问题标题】:getCurrentSession call fails right after openSessiongetCurrentSession 调用在 openSession 之后立即失败
【发布时间】:2012-12-10 02:20:29
【问题描述】:

您好,我已经使用 hibernate 配置了带有事务管理器的 sessionFactory 和 Mysql 数据源。当我尝试在 openSession() 之后立即对该工厂调用 getCurrentSession() 时,它会引发 HibernateException。我如何使它工作?

错误堆栈跟踪:

org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:978)
at com.xyz.CommonTest.initTx(CommonTest.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)

弹簧配置:

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="relationalDataSource"></property>
    <property name="packagesToScan">
        <list>
            <value>....</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}  </prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.order_updates">true</prop>
        </props>
    </property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

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

Java 代码:

    logger.info("initTx called");
    // Either get a current session or open new one.
    Session session = null;
    try {
        session = sessionFactory.getCurrentSession();
    } catch (HibernateException exception) {
        logger.info("did not find a current session, will try to open a new one",
                    exception);
    }
    session = sessionFactory.openSession();
    tx = session.beginTransaction();

    try {
        session = sessionFactory.getCurrentSession();
    } catch (HibernateException exception) {
        logger.info("did not find a current session, will try to open a new one",
                    exception);
    }

编辑:

您可以通过使用来做到这一点。

hibernate.current_session_context_class=线程 http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/session-configuration.html

如果您正在寻找一种自动执行此操作的方法,那么 Ryan 在下面的答案是正确的。

【问题讨论】:

    标签: java spring hibernate


    【解决方案1】:

    仅仅打开一个会话并不会将它与线程相关联。无论如何,您不应该手动打开会话。 Spring 应该为您打开和关闭会话 1) when a transaction starts and ends,或 2) 作为某种 open-session-in-view 实现的一部分,Spring 提供了 convenient implementations 。使用其中任何一种选择,您都可以确保会话得到妥善管理和清理,并且会话将与线程关联,因此您可以使用getCurrentSession()

    【讨论】:

    • 是的,我想通了。我需要使用 thread 配置来自动执行此操作。在 openSession 调用上。我需要手动调用它进行单元测试。我没有使用 spring 的注释来实现单元测试。这是一个简单的junit实现
    • 这可能不是您想要的,除非您将其设置为仅用于测试。自从我查看那部分代码已经有一段时间了,但我很确定这会对 Spring 对会话的管理造成严重破坏。如果你真的不想使用 Spring 的测试框架,这对于测试 DAO 来说非常棒,你应该能够使用 TransactionSynchronizationManager 手动绑定 Session 来绑定和取消绑定 Session。
    • 我已将其设置为一个属性,该属性是用于测试的线程,其余的则没有。不行吗?
    猜你喜欢
    • 2011-12-24
    • 2015-11-12
    • 2016-04-20
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    • 2021-07-08
    • 2016-10-24
    • 1970-01-01
    相关资源
    最近更新 更多