1

嗨,我已经使用 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=thread http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/session-configuration.html

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

4

1 回答 1

3

仅仅打开一个会话并不会将它与线程相关联。无论如何,您不应该手动打开会话。会话应该由 Spring 为您打开和关闭 1)在事务开始和结束时,或 2) 作为某种 open-session-in-view 实现的一部分,Spring 提供了方便的实现。使用这些选择中的任何一个,您都可以确保会话得到正确管理和清理,并且会话将与线程相关联,因此您可以使用getCurrentSession().

于 2012-12-23T03:36:01.843 回答