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
|