-->
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.  [ 3 posts ] 
Author Message
 Post subject: AbstractPersistentCollection.setCurrentSession
PostPosted: Fri Jun 24, 2005 3:00 am 
Beginner
Beginner

Joined: Tue Apr 20, 2004 12:07 pm
Posts: 21
I have incorporated some globaly used queries into a single thread safe class. This class is called on by numerous threads and each thread has its session stored in a ThreadLocal variable.
All the queries in the class use the uniqueResult() method.

--
Query query = currentSession().createQuery("select item from TDItem as item where item.id=:id");
query.setParameter("id", identifier);

return (TDItem) query.uniqueResult();
--

I was suprised to see this exception "org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions"
Cause all threads use there own session instance and no objects are shared between threads. Some further investigation revealed that a AbstractPersistentCollection collection instance is used by the uniqueResult() method and that this collection is reused between queries. I did not find any possibility to access the collection instance and make sure it is discarded between queries. There must be some way to specify this behaviour otherwise it would be impossible to execute the same query from multiple threads each with its own session.

So the question is, how can I specify that no collection instance is shared between queries or how do I gain access to the collection instance and discard it?

Brgds,

Eduard



Hibernate version: 3.0.5

org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
at org.hibernate.collection.AbstractPersistentCollection.setCurrentSession(AbstractPersistentCollection.java:237)
at org.hibernate.event.def.WrapVisitor.processCollection(WrapVisitor.java:43)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:104)
at org.hibernate.event.def.WrapVisitor.processValue(WrapVisitor.java:97)
at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:58)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:104)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:190)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:70)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at nl.tresparc.database.TransactionSupport.execute(TransactionSupport.java:38)


Name and version of the database you are using: PostgreSQL 8


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 3:13 am 
Beginner
Beginner

Joined: Tue Apr 20, 2004 12:07 pm
Posts: 21
I posted the wrong exception trace, this is the correct one. Sorry.

org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
at org.hibernate.collection.AbstractPersistentCollection.setCurrentSession(AbstractPersistentCollection.java:237)
at org.hibernate.event.def.WrapVisitor.processCollection(WrapVisitor.java:43)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:104)
at org.hibernate.event.def.WrapVisitor.processValue(WrapVisitor.java:97)
at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:58)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:104)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:190)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:70)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:39)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:711)
at org.hibernate.impl.SessionImpl.prepareQueries(SessionImpl.java:895)
at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:885)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:834)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:603)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 8:14 am 
Beginner
Beginner

Joined: Tue Apr 20, 2004 12:07 pm
Posts: 21
Thinking about the exception a little more draws me to the conclusion that the exception may not occur at all. A Query instance can only be created by means of a Session instance. The query is during its lifecycle bound to the same session. If the query is bound to the session, the result is also bound to the same session therefor the exception should never occur.

Can someone with insight to the internals of Hibernate elaborate on this and explain in what situation the exception can occur. Remember that I only use the uniqueResult() method.

Thank you.


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