gpt4 book ai didi

java - getCurrentSession 调用在 openSession 之后立即失败

转载 作者:行者123 更新时间:2023-11-29 05:50:37 25 4
gpt4 key购买 nike

您好,我已经使用 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)

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

最佳答案

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

关于java - getCurrentSession 调用在 openSession 之后立即失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14008161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com