Java 没有 Hibernate Session 绑定到线程,并且配置不允许在此处创建非事务性会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3289979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
提问by chedine
What is this error about? "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here". My spring config file looks something like this.
这个错误是关于什么的?“没有 Hibernate Session 绑定到线程,并且配置不允许在此处创建非事务性会话”。我的 spring 配置文件看起来像这样。
<bean id="jndiDataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:/devDS</value>
</property>
</bean>
<bean id="stsaDBFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="jndiDataSource" />
<property name="annotatedClasses">
<list>
<value>xx.yy.zz.User</value>
<value>xx.yy.UserResponse</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbmddl.auto">create</prop>
</props>
</property>
</bean>
<!-- ################################### Aspects ################################################## -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="stsaDBFactory" />
</property>
</bean>
All the DAO test passes when i test them outside of the container using junit. When I deploy it in jBoss as a portal app,I get this exception. Also it works fine if i remove the portal specific configuration and make it a simple web app and deploy it on jboss.Any idea?
当我使用 junit 在容器外测试它们时,所有 DAO 测试都通过了。当我将它作为门户应用程序部署在 jBoss 中时,出现此异常。如果我删除特定于门户的配置并使其成为一个简单的 Web 应用程序并将其部署在 jboss 上,它也可以正常工作。有什么想法吗?
采纳答案by rjsang
You have defined a TransactionManager in your spring config but you are trying to execute a hibernate query in a method that is not transactional. Try adding @Transactional to your method or class.
您已经在 spring 配置中定义了一个 TransactionManager,但您试图在非事务性方法中执行休眠查询。尝试将 @Transactional 添加到您的方法或类中。
回答by pprabhu
I got around this problem by specifying the current_session_context_class
in hibernate config
to be "thread
", as per the simple configuration shown in the hibernate configuration documentation.
根据休眠配置文档中显示的简单配置,我通过将current_session_context_class
in指定hibernate config
为“ thread
”来解决这个问题。
But it recommends that its not safe for production usage.
但它建议它对于生产使用不安全。
Trying to add the following in your hibernate config
should also help:
尝试在您的内容中添加以下内容hibernate config
也应该有所帮助:
<property name="current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
Check out http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/architecture.html#architecture-current-sessionfor more details.