“org.hibernate.HibernateException:非法尝试将集合与两个打开的会话相关联”即使上下文是 Thread

Posted

技术标签:

【中文标题】“org.hibernate.HibernateException:非法尝试将集合与两个打开的会话相关联”即使上下文是 Thread【英文标题】:"org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions" even when the context is Thread 【发布时间】:2013-11-06 02:33:48 【问题描述】:

在我的应用程序(服务器)中,我在请求处理开始时使用“sessionFactory.openSession()”创建了一个 Hibernate 会话。我在其他类中检索对象对其进行一些更改,然后执行saveOrUpdate,如下所示:

Session session = sessionFactory.getCurrentSession();
Transaction dbTransaction = session.getTransaction();
dbTransaction.begin();
session.saveOrUpdate(entity);
dbTransaction.commit()

我还在配置文件中设置了以下属性。

<property name="hibernate.current_session_context_class">thread</property>

但是,当我执行 session.saveOrUpdate(entity) 时,出现以下异常。我认为以下异常的原因是“getCurrentSession()”没有获得我使用“sessionFactory.openSession()”打开的会话,但我无法理解为什么它没有获得我使用“sessionFactory.openSession()”打开的 currentSession " 当请求处理在服务器中开始时。

错误堆栈跟踪:

    org.hibernate.HibernateException: Illegal attempt to associate a collection with
 two open sessions at org.hibernate.collection.internal.AbstractPersistentCollection.setCurrentSession(AbstractPersistentCollection.java:638)
    at org.hibernate.event.internal.OnUpdateVisitor.processCollection(OnUpdateVisitor.java:65)
    at org.hibernate.event.internal.AbstractVisitor.processValue(AbstractVisitor.java:121)
    at org.hibernate.event.internal.AbstractVisitor.processValue(AbstractVisitor.java:82)
    at org.hibernate.event.internal.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:76)
    at org.hibernate.event.internal.AbstractVisitor.process(AbstractVisitor.java:143)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:305)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:241)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:109)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:735)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:727)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:723)
    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:601)
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
    at com.sun.proxy.$Proxy24.saveOrUpdate(Unknown Source)
    at org.app.purchase.utils.PurchaseUtils.saveOrUpdateTransactionNew(PurchaseUtils.java:46)
    at org.app.purchase.model.Transaction.createRefundTransaction(Transaction.java:422)
    at org.app.purchase.application.RefundTransaction.refundTransaction(RefundTransaction.java:64)
    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:601)
    at org.restlet.resource.ServerResource.doHandle(ServerResource.java:449)
    at org.restlet.resource.ServerResource.get(ServerResource.java:645)
    at org.restlet.resource.ServerResource.doHandle(ServerResource.java:527)
    at org.restlet.resource.ServerResource.doNegotiatedHandle(ServerResource.java:587)
    at org.restlet.resource.ServerResource.doConditionalHandle(ServerResource.java:299)
    at org.restlet.resource.ServerResource.handle(ServerResource.java:846)
    at org.restlet.resource.Finder.handle(Finder.java:510)
    at org.restlet.routing.Filter.doHandle(Filter.java:156)
    at org.restlet.routing.Filter.handle(Filter.java:203)
    at org.restlet.routing.Router.doHandle(Router.java:497)
    at org.restlet.routing.Router.handle(Router.java:737)
    at org.restlet.routing.Filter.doHandle(Filter.java:156)
    at org.restlet.routing.Filter.handle(Filter.java:203)
    at org.restlet.routing.Filter.doHandle(Filter.java:156)
    at org.restlet.routing.Filter.handle(Filter.java:203)
    at org.restlet.routing.Filter.doHandle(Filter.java:156)
    at org.restlet.engine.application.StatusFilter.doHandle(StatusFilter.java:151)
    at org.restlet.routing.Filter.handle(Filter.java:203)
    at org.restlet.routing.Filter.doHandle(Filter.java:156)
    at org.restlet.routing.Filter.handle(Filter.java:203)
    at org.restlet.engine.ChainHelper.handle(ChainHelper.java:111)
    at org.restlet.engine.application.ApplicationHelper.handle(ApplicationHelper.java:72)
    at org.restlet.Application.handle(Application.java:388)
    at org.restlet.routing.Filter.doHandle(Filter.java:156)
    at org.restlet.routing.Filter.handle(Filter.java:203)
    at org.restlet.routing.Router.doHandle(Router.java:497)
    at org.restlet.routing.Router.handle(Router.java:737)
    at org.restlet.routing.Filter.doHandle(Filter.java:156)
    at org.restlet.routing.Filter.handle(Filter.java:203)
    at org.restlet.routing.Router.doHandle(Router.java:497)
    at org.restlet.routing.Router.handle(Router.java:737)
    at org.restlet.routing.Filter.doHandle(Filter.java:156)
    at org.restlet.engine.application.StatusFilter.doHandle(StatusFilter.java:151)
    at org.restlet.routing.Filter.handle(Filter.java:203)
    at org.restlet.routing.Filter.doHandle(Filter.java:156)
    at org.restlet.routing.Filter.handle(Filter.java:203)
    at org.restlet.engine.ChainHelper.handle(ChainHelper.java:111)
    at org.restlet.Component.handle(Component.java:388)
    at org.restlet.Server.handle(Server.java:488)
    at org.restlet.engine.http.connector.BaseServerHelper.handle(BaseServerHelper.java:158)
    at org.restlet.engine.http.connector.BaseServerHelper.handleInbound(BaseServerHelper.java:167)
    at org.restlet.engine.http.connector.BaseHelper.handleNextInbound(BaseHelper.java:418)
    at org.restlet.engine.http.connector.Connection.readMessages(Connection.java:695)
    at org.restlet.engine.http.connector.Controller$2.run(Controller.java:95)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

【问题讨论】:

你在使用 Spring 吗? 不,我没有使用 Spring。 知道为什么会出现这个错误吗? 【参考方案1】:

调用openSession() 不会将生成的 Session 对象与 Hibernate 的当前会话上下文相关联。但是,如果您调用 getCurrentSession() 并且不存在会话,那么 Hibernate 将打开一个新会话并将其与上下文相关联。

换句话说,您需要将您对openSession() 的调用替换为对getCurrentSession() 的调用。

【讨论】:

我将 openSession() 方法调用更改为 getCurrentSession() 但我现在面临不同的问题。我实体中的集合被延迟初始化。因此,当我尝试访问实体的任何延迟初始化字段时,我得到“org.hibernate.SessionException:会话已关闭!” .有什么办法可以避免这个问题? 你在哪里关闭会话?您要转移到其他线程吗? 不,我根本不关闭会话。但它会抛出此异常“org.hibernate.SessionException:会话已关闭!”。仅当我使用“ sessionFactory.getCurrentSession() ”时才会发生这种情况。但是使用“ sessionFactory.openSession() ”它以正确的方式工作。 那是因为 ThreadLocalSessionContext 在你提交事务时关闭了会话。如果您想使用 current_session_context_class 的特定实现,那么您将需要在您正在使用的会话的生命周期内拥有一个事务。

以上是关于“org.hibernate.HibernateException:非法尝试将集合与两个打开的会话相关联”即使上下文是 Thread的主要内容,如果未能解决你的问题,请参考以下文章