java 当前没有会话绑定到执行上下文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30212125/
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 session currently bound to execution context
提问by Srinivas
I got below exception when I used session.getCurrentSession()
.
当我使用session.getCurrentSession()
.
I have mentioned
我已经提到
hibernate.current_session_context_class: managed
org.hibernate.HibernateException: No session currently bound to execution context
at org.hibernate.context.internal.ManagedSessionContext.currentSession(ManagedSessionContext.java:75)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
at io.dropwizard.hibernate.AbstractDAO.currentSession(AbstractDAO.java:36)
at io.dropwizard.hibernate.AbstractDAO.persist(AbstractDAO.java:149)
I use this with dropwizard
. Can anyone help me to solve this?
我将它与dropwizard
. 谁能帮我解决这个问题?
回答by yunspace
If you are using Dropwizard Hibernate. You need to add @UnitOfWork
annotation to your Resource method. More info within dropwizard manual, hibernate chapter.
如果您使用的是 Dropwizard Hibernate。您需要为@UnitOfWork
Resource 方法添加注释。dropwizard 手册中的更多信息,休眠章节。
回答by Arpit Aggarwal
Can you try with : session.openSession()
- It tell hibernate to always opens a new session and you have to close once you are done with the operations. With session.getCurrentSession()
, hibernate returns a session bound to a context that you don't need to close and only need to set the hibernate.current_session_context_class to thread.
您可以尝试:session.openSession()
- 它告诉 hibernate 始终打开一个新会话,并且在完成操作后必须关闭。使用session.getCurrentSession()
,hibernate 返回绑定到上下文的会话,您不需要关闭该上下文,只需将 hibernate.current_session_context_class 设置为线程。
You can also configure session with SpringSessionContext
, if your application is Spring based.
SpringSessionContext
如果您的应用程序是基于 Spring 的,您还可以使用 配置会话。
Edit your hibernate-cfg.xml
with below line:
hibernate-cfg.xml
使用以下行编辑您的:
hibernate.current_session_context_class=org.springframework.orm.hibernate3.SpringSessionContext
What above line will do?
上面的线会做什么?
Making session context class as "org.springframework.orm.hibernate3.SpringSessionContext
", Hibernate will assume it is executing inside of a Spring transactional context (i.e. through a Spring transactional aspect) and Spring will now manage your transaction for you. However if you call getCurrentSession()
outside of such a context, Hibernate will throw an exception complaining that no Session is bound to the thread.
将会话上下文类设为“org.springframework.orm.hibernate3.SpringSessionContext”,Hibernate 将假定它在 Spring 事务上下文内执行(即通过 Spring 事务方面),Spring 现在将为您管理您的事务。然而,如果你getCurrentSession()
在这样的上下文之外调用,Hibernate 将抛出一个异常,抱怨没有 Session 绑定到线程。