当前没有会话绑定到执行上下文
当我使用session.getCurrentSession()
时,我得到了以下exception。
我已经提到了
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)
我用它与dropwizard。 任何人都可以帮我解决这个问题吗?
如果你使用Dropwizard Hibernate。 您需要将@UnitOfWork
注释添加到Resource方法中。 有关详细信息,请参阅dropwizard手册, hibernate章节 。
你可以尝试使用: session.openSession()
– 它告诉hibernate总是打开一个新的会话,你必须在完成操作后关闭。 使用session.getCurrentSession()
,hibernate返回绑定到您不需要关闭的上下文的会话,只需要将hibernate.current_session_context_class设置为thread。
如果您的应用程序是基于Spring的,您还可以使用SpringSessionContext
配置会话。
使用以下行编辑hibernate-cfg.xml
:
hibernate.current_session_context_class=org.springframework.orm.hibernate3.SpringSessionContext
以上行将做什么?
将会话上下文类设为“org.springframework.orm.hibernate3.SpringSessionContext” ,Hibernate将假设它在Spring事务上下文中执行(即通过Spring事务方面),Spring现在将为您管理事务。 但是,如果在这样的上下文之外调用getCurrentSession()
,Hibernate将抛出一个exception,抱怨没有Session绑定到该线程。