-->
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.  [ 2 posts ] 
Author Message
 Post subject: ConcurrentModificationException when deleting from a coll.
PostPosted: Tue Jun 22, 2004 11:55 am 
Newbie

Joined: Tue Jun 22, 2004 11:22 am
Posts: 1
I've run into a seemingly simple problem relating to deletion of objects in a collection I'm iterating through.

The setup is a very simple one-many, where the code below fails with a java.util.ConcurrentModificationException.

Code:
  Iterator childIterator = getChildren().iterator();
  while (iter.hasNext()) {
  ((Child) iter.next()).delete(session);
  }


The getChildren method returns a set of children which is being iterated through, and a method "delete" is called on each child. The delete method updates other objects and an index, and possibly the child deletes itself by calling session.delete(this)

Everything works fine aslong as there's only one child. :) But if there are more, and one of the children is deleted, the childIterator/childSet is changed, and and the following call to iter.next() fails with the above exception. This might be expected since I'm delete from the collection I'm working on, and I understand why this is a problem, I just hope there's an easy solution. :)

If I convert the Set to an array, and runs through that, everything works fine, but that seems a bit too ugly for my taste.

PS I know for sure that only one thread is modifying the collection.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 22, 2004 12:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You can not modify a colection while you are iterating over it. This is not a Hibernate issue.


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