openSession之后,getCurrentSession调用立即失败

时间:2012-12-23 02:27:19

标签: java spring hibernate

您好我使用hibernate配置了事务管理器的sessionFactory和MysqL数据源。当我尝试在openSession()之后立即在该工厂上调用getCurrentSession()时,它会抛出HibernateException。我如何使其工作?

Stacktrace错误:

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)

Spring配置:

<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在下面的回答是正确的。

1 个答案:

答案 0 :(得分:3)

仅打开会话不会将其与线程相关联。无论如何,您不应该手动打开会话。 Spring应该为您打开和关闭会话1)when a transaction starts and ends或2)作为某种开放式会话视图实现的一部分,Spring提供convenient implementations。通过这些选择中的任何一个,您可以确保会话得到妥善管理和清理,并且可以使用与该线程关联的会话,以便您可以使用getCurrentSession()