-->
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.  [ 7 posts ] 
Author Message
 Post subject: Exception with update() and many sessions
PostPosted: Tue Apr 18, 2006 10:10 am 
Newbie

Joined: Tue Apr 18, 2006 9:57 am
Posts: 18
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp


I use hibernate with web application, when i open many session begin the problems with update and save. this is the information:

[b]Hibernate version: I used hibernate 3

[b]Code between sessionFactory.openSession() and session.close():

session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
session.update(nuevo);
session.flush();
tx.commit();


[b]Full stack trace of any exception that occurs:

org.hibernate.HibernateException: illegally attempted to associate a proxy with two open Sessions
at org.hibernate.proxy.AbstractLazyInitializer.setSession(AbstractLazyInitializer.java:68)
at org.hibernate.engine.PersistenceContext.reassociateProxy(PersistenceContext.java:520)
at org.hibernate.engine.PersistenceContext.unproxyAndReassociate(PersistenceContext.java:565)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:65)
at org.hibernate.impl.SessionImpl.update(SessionImpl.java:499)
at org.hibernate.impl.SessionImpl.update(SessionImpl.java:490)
at com.vinculolegal.servicio.ServicioBD.update(ServicioBD.java:809)
at com.vinculolegal.action.IndicadorAction.execute(IndicadorAction.java:94)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:300)
at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:374)
at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:743)
at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:675)
at org.apache.jk.common.SocketConnection.runIt(ChannelSocket.java:866)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:534)




[b]Name and version of the database you are using: Postgres database 8.0


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 10:23 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Where do you close your session?


Top
 Profile  
 
 Post subject: its necesary
PostPosted: Tue Apr 18, 2006 11:20 am 
Newbie

Joined: Tue Apr 18, 2006 9:57 am
Posts: 18
Its necesary close the session ? when i open the current session?

Like that:


session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
session.update(nuevo);
session.flush();
tx.commit();

--->>>> session.close();


the session dont lost?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 12:54 pm 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
It depends on what you are trying to do.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 1:06 pm 
Beginner
Beginner

Joined: Thu Aug 19, 2004 2:33 pm
Posts: 30
Location: CA, USA
I'm not sure why you got that Exception, but this is something to think about.

Are you using the Thread local session? In other words, in your hibernate.cfg.xml do you have:

Code:
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>


If so, then each thread has it's own copy of the Session. In a web application , the web container has a pool of threads handling requests. Each will have it's own Hibernate Session. If you are storing hibernate managed objects in the HTTP Session and then trying to update your database, you aren't guarenteed subsequent HTTP requests will be handled by the same servlet thread and thus the same Hibernate Session.

Let me know if this isn't clear. But I suspect you may be storing objects in the HTTP Session and then trying to update them in a different servlet thread.


Top
 Profile  
 
 Post subject: servlets+hibernate
PostPosted: Tue Apr 18, 2006 3:33 pm 
Newbie

Joined: Tue Apr 18, 2006 9:57 am
Posts: 18
Yes, I load one object and save in the session HTTP: request.getSession().setAttribute("obj", obj);

In the three next servlet, i update the object and throw the exception, how i manage this situation? what is the best way to do that.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 7:16 pm 
Beginner
Beginner

Joined: Thu Aug 19, 2004 2:33 pm
Posts: 30
Location: CA, USA
I've just started using Hibernate 3 in a web environment so takes these as suggestions.

1. I've read that some people put the hibernate Session into the HTTP Session and then retrieve it in the next request. The only problem I see with this is that the session could have a lot of stuff cached in it and you may run into memory problems. But that depends on your applicaiton. you could always evict() objects from the cache or close the session at certain points. The Hibernate Wiki has a section on "Long Running Conversations" that's probably worth reading. Also see the "Open Session In View" pattern on the Wiki.

2. Another option is to disconnect the objects from the hibernate session, put them in the HTTP Session, and then reattach them. Your best bet here is to read the Hibernate document on disconnecting and reattaching objects. I think it's short. This isn't much work and might be a good option.

3. A third option is to just do a database hit when the new HTTP request comes in and get a new copy of the object, modify that, and persist it. In this case you would store just the unique identifier in the HTTP session since you don't need the whole object.

There's probably some other ways, but hopefully this will give you some ideas. Sorry I don't have a definitive answer. I've basically been using the 3rd option, but my web app is very simple, with few users, and a low volume of requests (at least for now).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.