title: "org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions" even when the context is Thread
body: <p>In my application(server) I create a Hibernate session in the beginning of request processing using "<code>sessionFactory.openSession()</code>". I retrieve the object make some changes to it in some other class and then do a <code>saveOrUpdate</code> as follows:</p>
  
  <pre><code>Session session = sessionFactory.getCurrentSession();
  Transaction dbTransaction = session.getTransaction();
  dbTransaction.begin();
  session.saveOrUpdate(entity);
  dbTransaction.commit()
  </code></pre>
  
  <p>I have also set the below property in the configuration file.   </p>
  
  <pre><code>&lt;property name="hibernate.current_session_context_class"&gt;thread&lt;/property&gt;
  </code></pre>
  
  <p>However when I do a <code>session.saveOrUpdate(entity)</code> I get the below exception. I think the reason for the below exception is "<code>getCurrentSession()</code>" is not getting the session which I opened using "<code>sessionFactory.openSession()</code>"  but I am not able to understand why it is not getting the currentSession which I opened using "<code>sessionFactory.openSession()</code>" when the request processing begins in the server.   </p>
  
  <p><strong>Error stack trace:</strong></p>
  
  <pre><code>    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)
  </code></pre>