-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: session reattach problem
PostPosted: Thu Feb 26, 2004 11:28 am 
Beginner
Beginner

Joined: Sat Jan 31, 2004 10:09 am
Posts: 26
Let's see the source snapshot first:
Code:
   public boolean contains(Object object) {
      if (object instanceof HibernateProxy) {
         return HibernateProxyHelper.getLazyInitializer( (HibernateProxy) object ).getSession()==this;
      }
      else {
         return entityEntries.containsKey(object);
      }
   }


Above is catched from SessionImpl.

My problem is that I would like to reattach a seesion(opened in the second request-response cycle) to an entity(queried out in the first request-respponse cycle). But before I reattach a session, I think it is necessary to test if this entity is already associated with a session.
Here the problem comes, actually my entity is an instance of HibernateProxy and it is still associated with a first session opened in the first request-response cycle. But I believe I already close the first session and set the threadlocal to null in Servlet Filter. So this session should be supposed no loger existed. But actually it is still referenced by this entity, right? Why I said so, it is because the
Code:
HibernateProxyHelper.getLazyInitializer( (HibernateProxy) object ).getSession()
returns a session which is supposed to be closed and should be detached from the entity. But since the old session is returned and absolutely not equals to the new session, so it return false.

Because contains() return false, I tried to reattach a new session to this entity. And then it results in this exception:
Code:
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 1, of class: kk.Employee


Is the statement above correct? Or something I missed, so resulted in such a problem?
Below is my servlet filter to manage session, for your reference:
Code:
public class KKFilter implements Filter {

   private static ThreadLocal hibernateSession = new ThreadLocal();
   private static SessionFactory sessionFactory;
   
   static {
      
      Configuration cfg;
      try {
         cfg = new Configuration().configure();
         sessionFactory = cfg.buildSessionFactory();
      }
      catch (HibernateException e) {
         throw new RuntimeException("Fail to configure hibernate", e);
      }
   }
   
   public static Session getSession() {
      
      Session s = (Session) hibernateSession.get();
      if (s == null || !s.isOpen()) //This is usually used without Servlet
         s = initSession();
      return s;
   }
   
   public void init(FilterConfig arg0) throws ServletException {
      
      // empty
   }

   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
   
      assert hibernateSession.get() == null;
      
      try {
         Session session = initSession();

         String result = null;
         try {
            chain.doFilter(request, response);
         }
         finally {
            session = (Session) hibernateSession.get();
            if (session != null)
               session.close();
            hibernateSession.set(null);
         }
      }
      catch (HibernateException e) {
         throw new RuntimeException("Fail to close hibernate session", e);
      }
   }

   public void destroy() {
   
   }
   
   private static Session initSession() {
      
      Session session;
      try {
         session = sessionFactory.openSession();
         hibernateSession.set(session);
         return session;
      }
      catch (HibernateException e) {
         throw new RuntimeException("Fail to open hibernate session.");
      }
      
   }
}
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 26, 2004 10:28 pm 
Beginner
Beginner

Joined: Sat Jan 31, 2004 10:09 am
Posts: 26
This problem gets me in trouble, I don't know it is my problem or Hibernate's problem. Thank you very much for any help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 26, 2004 10:53 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
You need to associate the proxy from the first session with the second session. I don't use the "disconnected object" support (which is what you are trying to do), but I believe the syntax you want is session.lock(myProxy, LockMode.NONE)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 28, 2004 8:44 am 
Beginner
Beginner

Joined: Sat Jan 31, 2004 10:09 am
Posts: 26
Thanks Steve,
Sorry I can't figure what "You need to associate the proxy from the first session with the second session. I don't use the 'disconnected object' support" means.
I did call the session.lock(myProxy, LockMode.NONE), and I believe lock has some limits, too.
Let me answer my question myself.
I said while I called the session.lock(), I got net.sf.hibernate.NonUniqueObjectException, right? I finally know the reason.
In my application, I need to keep the login info , "entity.Login" for example, during the HTTP session life cycle. And during the HTTP session life cycle, there maybe some transactions occur and load the entity.Login as well, of course entity.Logn will be keeped in the entry map of current Hibernate session. (I close Hibernate session after each request-response cycle)
Because entity.Login also associates with entity.Organization, and organization is also a lazy loading proxy. So before I can call the login.getOrganization() method, I need reattach session to entity.Login, right. Then you know the result, I got the NonUniqueObjectException, because there is already an entity.Login with the same identifier contained in the current Hibernate session entry map. And since entity.Login is a proxy, so of course session.contains() return false. That's why I got the unfortunately exception.....
About another session.lock() problem, I post it in next thread.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 06, 2006 2:56 pm 
Newbie

Joined: Tue Sep 16, 2003 3:26 pm
Posts: 8
Check:

http://forum.hibernate.org/viewtopic.ph ... 26#2284826

For a possible solution in Hibernate 3.0+.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.