-->
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.  [ 8 posts ] 
Author Message
 Post subject: Illegal attempt to associate a collection with two open sess
PostPosted: Thu Aug 06, 2009 12:49 pm 
Regular
Regular

Joined: Fri May 22, 2009 4:50 am
Posts: 59
hi all!

I am using hibernate and spring and all my DAOs extend HibernateDAOSupport.
I have an entity 'Order' which contains an attribute 'Quote' and Quote contains collections of 'Buy' and 'Sell' values.

<class name="Order" table="ORDERS" lazy="false">
<id name="objectId" column="ORDER_UUID">
<generator class="uuid.hex"/>
</id>
<!-- for Quote -->
<many-to-one name="Quote" class="Quote"
column="quote_id"
cascade="all"
unique="true"/>

</class>

<class name="Quote" table="QUOTE">
<id name="id" column="QUOTE_ID">
<generator class="sequence">
<param name="sequence">quote_sequence</param>
</generator>
</id>
<map name="sell" table="sell" sort="natural">
<key column="quote_id"/>
<index column="price"/>
---------
</map>
<map name="buy" table="buy" sort="natural">
<key column="quote_id"/>
<index column="price"/>
---------
</map>
</class>

As you must have noticed, I am using lazy="false" and cascade="all" in Order mapping. So my Order object has all its collection initialized.

Now problem is when I stress test my code to store and update an order it throws the following error:
Illegal attempt to associate a collection with two open sessions

I Suppose reason is, the collections are not versioned like objects so when the same collection is opened with multiple sessions HB doesn't know who's going to win, as mentioned by Costin Leau in the following thread,
http://forum.springsource.org/showthread.php?t=26782

Can any one please suggest me solution to it?
Any help is appreciated.

Thanks


Top
 Profile  
 
 Post subject: Re: Illegal attempt to associate a collection with two open sess
PostPosted: Thu Aug 06, 2009 3:39 pm 
Beginner
Beginner

Joined: Wed Jul 29, 2009 3:43 pm
Posts: 22
Synchronize access to the method that's doing save or update.


Top
 Profile  
 
 Post subject: Re: Illegal attempt to associate a collection with two open sess
PostPosted: Fri Aug 07, 2009 7:33 am 
Regular
Regular

Joined: Fri May 22, 2009 4:50 am
Posts: 59
Thanks Jim.
I think that will work.


Top
 Profile  
 
 Post subject: Re: Illegal attempt to associate a collection with two open sess
PostPosted: Fri Aug 07, 2009 8:15 am 
Beginner
Beginner

Joined: Wed Jul 29, 2009 3:43 pm
Posts: 22
you're most welcome.


Top
 Profile  
 
 Post subject: Re: Illegal attempt to associate a collection with two open sess
PostPosted: Fri Aug 07, 2009 11:38 am 
Regular
Regular

Joined: Fri May 22, 2009 4:50 am
Posts: 59
Hi,
Problem is not solved completely as i thought it to be.
I have synchrinozed my DAO method which calls getHibernateTemplate().saveOrUpdate(order) which solved the two session problem.
But I am getting java.util.ConcurrentModificationException as follows:

15:29:34.831 [AnonymousIoService-10] ERROR errors Exception caught!
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.miriamlaurel.pms.listeners.dispatch.DispatchListener.invoke(DispatchListener.java:175) [miriamlaurel-pms-1.0-SNAPSHOT.jar:na]
at com.miriamlaurel.pms.listeners.dispatch.DispatchListener.processMessage(DispatchListener.java:111) [miriamlaurel-pms-1.0-SNAPSHOT.jar:na]
at com.miriamlaurel.blackjack.server.SessionManager.messageReceived(SessionManager.java:385) [classes/:na]
at com.miriamlaurel.blackjack.server.SessionManager.messageReceived(SessionManager.java:1) [classes/:na]
at org.apache.mina.handler.demux.DemuxingIoHandler.messageReceived(DemuxingIoHandler.java:141) [mina-core-1.1.7.jar:na]
at org.apache.mina.common.support.AbstractIoFilterChain$TailFilter.messageReceived(AbstractIoFilterChain.java:570) [mina-core-1.1.7.jar:na]
at org.apache.mina.common.support.AbstractIoFilterChain.callNextMessageReceived(AbstractIoFilterChain.java:299) [mina-core-1.1.7.jar:na]
at org.apache.mina.common.support.AbstractIoFilterChain.access$1100(AbstractIoFilterChain.java:53) [mina-core-1.1.7.jar:na]
at org.apache.mina.common.support.AbstractIoFilterChain$EntryImpl$1.messageReceived(AbstractIoFilterChain.java:648) [mina-core-1.1.7.jar:na]
at org.apache.mina.filter.codec.support.SimpleProtocolDecoderOutput.flush(SimpleProtocolDecoderOutput.java:58) [mina-core-1.1.7.jar:na]
at org.apache.mina.filter.codec.ProtocolCodecFilter.messageReceived(ProtocolCodecFilter.java:180) [mina-core-1.1.7.jar:na]
at org.apache.mina.common.support.AbstractIoFilterChain.callNextMessageReceived(AbstractIoFilterChain.java:299) [mina-core-1.1.7.jar:na]
at org.apache.mina.common.support.AbstractIoFilterChain.access$1100(AbstractIoFilterChain.java:53) [mina-core-1.1.7.jar:na]
at org.apache.mina.common.support.AbstractIoFilterChain$EntryImpl$1.messageReceived(AbstractIoFilterChain.java:648) [mina-core-1.1.7.jar:na]
at org.apache.mina.filter.executor.ExecutorFilter.processEvent(ExecutorFilter.java:220) [mina-core-1.1.7.jar:na]
at org.apache.mina.filter.executor.ExecutorFilter$ProcessEventsRunnable.run(ExecutorFilter.java:264) [mina-core-1.1.7.jar:na]
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [na:1.6.0_13]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [na:1.6.0_13]
at org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:51) [mina-core-1.1.7.jar:na]
at java.lang.Thread.run(Thread.java:619) [na:1.6.0_13]
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [na:1.6.0_13]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [na:1.6.0_13]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [na:1.6.0_13]
at java.lang.reflect.Method.invoke(Method.java:597) [na:1.6.0_13]
at com.miriamlaurel.pms.listeners.dispatch.DispatchListener.invoke(DispatchListener.java:171) [miriamlaurel-pms-1.0-SNAPSHOT.jar:na]
... 19 common frames omitted
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.miriamlaurel.pms.listeners.dispatch.DispatchListener.invoke(DispatchListener.java:175) [miriamlaurel-pms-1.0-SNAPSHOT.jar:na]
at com.miriamlaurel.pms.listeners.dispatch.DispatchListener.processMessage(DispatchListener.java:111) [miriamlaurel-pms-1.0-SNAPSHOT.jar:na]
at com.miriamlaurel.blackjack.server.SessionManager.onMessage(SessionManager.java:177) [classes/:na]
... 24 common frames omitted
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [na:1.6.0_13]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [na:1.6.0_13]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [na:1.6.0_13]
at java.lang.reflect.Method.invoke(Method.java:597) [na:1.6.0_13]
at com.miriamlaurel.pms.listeners.dispatch.DispatchListener.invoke(DispatchListener.java:171) [miriamlaurel-pms-1.0-SNAPSHOT.jar:na]
... 26 common frames omitted
Caused by: java.util.ConcurrentModificationException
at java.util.TreeMap$PrivateEntryIterator.nextEntry(TreeMap.java:1100) [na:1.6.0_13]
at java.util.TreeMap$EntryIterator.next(TreeMap.java:1136) [na:1.6.0_13]
at java.util.TreeMap$EntryIterator.next(TreeMap.java:1131) [na:1.6.0_13]
at org.hibernate.persister.collection.BasicCollectionPersister.doUpdateRows(BasicCollectionPersister.java:206) [hibernate-3.3.1.GA.jar:na]
at org.hibernate.persister.collection.AbstractCollectionPersister.updateRows(AbstractCollectionPersister.java:1541) [hibernate-3.3.1.GA.jar:na]
at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:85) [hibernate-3.3.1.GA.jar:na]
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279) [hibernate-3.3.1.GA.jar:na]
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263) [hibernate-3.3.1.GA.jar:na]
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:170) [hibernate-3.3.1.GA.jar:na]
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) [hibernate-3.3.1.GA.jar:na]
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50) [hibernate-3.3.1.GA.jar:na]
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027) [hibernate-3.3.1.GA.jar:na]
at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390) [spring-2.5.4.jar:2.5.4]
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:420) [spring-2.5.4.jar:2.5.4]
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) [spring-2.5.4.jar:2.5.4]
at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:744) [spring-2.5.4.jar:2.5.4]
at com.tsdevelopment.blackjack.server.dao.impl.hibernate.OrderDaoImpl.storeOrder(OrderDaoImpl.java:32) [classes/:na]
at com.miriamlaurel.blackjack.server.services.DataManager.storeOrder(DataManager.java:55) [classes/:na]
at com.miriamlaurel.blackjack.server.services.SignalOrderProcessor.changeOrderState(SignalOrderProcessor.java:315) [classes/:na]
at com.miriamlaurel.blackjack.server.services.SignalOrderProcessor.executeOrder(SignalOrderProcessor.java:97) [classes/:na]
at com.miriamlaurel.blackjack.server.services.SignalOrderProcessor.putOrder(SignalOrderProcessor.java:87) [classes/:na]
at com.miriamlaurel.blackjack.server.services.SignalOrderProcessor.process(SignalOrderProcessor.java:393) [classes/:na]
at com.miriamlaurel.blackjack.server.dispatch.AccountDispatcher.onSignal(AccountDispatcher.java:134) [classes/:na]
... 31 common frames omitted

Now since I have synchronized the saveOrUpdate method I am unable to understand thsi exception.
I feel probably this is not the best way to handle collection in hibernate.
Can any one please guide me with the correct approach to solve thsi problem.

Any help appreciated!

Thanks again...


Top
 Profile  
 
 Post subject: Re: Illegal attempt to associate a collection with two open sess
PostPosted: Sat Aug 08, 2009 4:34 am 
Beginner
Beginner

Joined: Wed Jul 29, 2009 3:43 pm
Posts: 22
This exception has got nothing to do with synchronization. And probably nothing to do with Hibernate as well. It looks like that you are changing some collection as you are iterating over it. Please atleast try to debug the problem before jumping to conclusions.


Top
 Profile  
 
 Post subject: Re: Illegal attempt to associate a collection with two open sess
PostPosted: Mon Aug 10, 2009 5:12 am 
Regular
Regular

Joined: Fri May 22, 2009 4:50 am
Posts: 59
thanks for your reply.
I agree its a java collecion error which has nothing to do with hibernate.
The exception is actually being thrown from OrderDaoImpl.storeOrder in code that has to do with collections in quote.price.
Following is my code for OrderDaoImpl.storeOrder:

Code:
public synchronized void storeOrder(ClientOrder order) {
      Integer id = order.getId();
      if(id == null){
            order.setId(idGenerator.nextId());
        }         
   getHibernateTemplate().saveOrUpdate(order);         
}


SO point is I dont handle any of these collections myself. They are handled(save or updated) by hibernate on call of saveOrUpdate of order.
Also i want to confirm if this is how every one handles collections in case of cascade


Top
 Profile  
 
 Post subject: Re: Illegal attempt to associate a collection with two open sess
PostPosted: Sat Aug 15, 2009 7:06 pm 
Newbie

Joined: Sat Aug 15, 2009 7:00 pm
Posts: 2
I also still new to hibernate, but, I think your problem is in opening a session twice, as i encountered this problem before.

You should grantee that there is a single session you use in your operations.

for example, you may use HibernateUtil.getSessionFactory().getCurrentSession(); to obtain a new session and you make sure that you don't close it until you do with all your operations needed for this session.

I hope you get it, thanks.


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