-->
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: ConcurrentModificationException thrown in multi-thread env
PostPosted: Thu Jan 19, 2012 3:33 am 
Newbie

Joined: Thu Jan 19, 2012 3:14 am
Posts: 3
Hi all,

I encounter a problem in getting ConcurrentModificationException when there is more than 1 sessions opened concurrently:.

Code:
java.util.ConcurrentModificationException
   at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(LinkedHashMap.java:373)
   at java.util.LinkedHashMap$EntryIterator.next(LinkedHashMap.java:392)
   at java.util.LinkedHashMap$EntryIterator.next(LinkedHashMap.java:391)
   at org.hibernate.util.IdentityMap.entryArray(IdentityMap.java:228)
   at org.hibernate.util.IdentityMap.concurrentEntries(IdentityMap.java:89)
   at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:205)
   at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
   at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:58)
   at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:1185)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1261)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
   at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:246)
...


I am coding against JPA API that EntityManager is referred to manipulate objects. Corresponding entityManager is stored in thread local that its session as well as the persistence context should be threadsafe.

However, there are something weird that I found some debug messages from hibernate on child collection initialization about the parent objects retrieved from thread A appeared in thread B makes me believe that corresponding collection initialization was done under thread B which is not expected because thread A and thread B is working on independent hierarchy of objects. And that's why causing the ConcurrentModificationException when thread B is trying to flush its own.

Does anyone offer me some helps on it as I have being working on it for days but still no clue?


Top
 Profile  
 
 Post subject: Re: ConcurrentModificationException thrown in multi-thread env
PostPosted: Thu Jan 19, 2012 9:54 pm 
Newbie

Joined: Thu Jan 19, 2012 3:14 am
Posts: 3
Hi all,

env info:

Hibernate: 3.6.9.Final
OS: Redhat 5
JDK: 1.6.0


Top
 Profile  
 
 Post subject: Re: ConcurrentModificationException thrown in multi-thread env
PostPosted: Wed Jan 25, 2012 9:53 pm 
Newbie

Joined: Thu Jan 19, 2012 3:14 am
Posts: 3
Hi all,

Eventually, I spot out that the unexpected modification in thread A is come from the forceInitialization on the PersistentCollection in thread B in my own program before I pass the object from server to client. According to my debug logging, I think that the PersistentCollection is not threadsafe because the associated session and persistence context which the PersistentCollection associated to, is belongs to the session of thread A!

I have written a testing program to simulate the problem. I coded to retrieve a parent object with child objects in oneToMany relationship and in LAZY fetch. After that, my intialization program will initialize corresponding child collection traversely if it is not initialized (in type of PersistentCollection) by calling forceInitialization. At this moment, I debug out the uuid of the session and persistence context the PersistentCollection is associating to.

I run the testing program by creating 5 threads running the same code fragment concurrently and get the odd debug logs as below proving my thought of the non-thread safe PersistentCollection.

14:55:44.751 [Retrieval Thread [4]] DEBUG MyInitializer - Force initiailize in thread 15595312 persistent collection 15976046 in session 15740343 in context 4809601
14:55:47.282 [Retrieval Thread [3]] DEBUG MyInitializer - Force initiailize in thread 24791433 persistent collection 4835957 in session 15740343 in context 4809601
14:55:47.329 [Retrieval Thread [3]] DEBUG MyInitializer - Force initiailize in thread 24791433 persistent collection 19185231 in session 15740343 in context 4809601
14:55:47.923 [Retrieval Thread [2]] DEBUG MyInitializer - Force initiaiize in thread 13986615 persistent collection 14869148 in session 15740343 in context 4809601
14:55:48.891 [Retrieval Thread [4]] DEBUG MyInitializer - Force initiaiize in thread 15595312 persistent collection 4835957 in session 15740343 in context 4809601
14:55:48.891 [Retrieval Thread [4]] DEBUG MyInitializer - Force initiaiize in thread 15595312 persistent collection 19185231 in session 15740343 in context 4809601
14:55:48.891 [Retrieval Thread [4]] DEBUG MyInitializer - Force initiaiize in thread 15595312 persistent collection 23809882 in session 15740343 in context 4809601

I am sure that entityManager who holds the session is well managed in my program and threadsafe. But how come the PersistentCollection refer to the wrong session? Is it no longer for us to work on PersistentCollection by our own? However, it is the same way to initialize the object by calling Hibernate.initialize(). Do any of you have idea on it?


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.