If a single thread issues a sequence of method invocations that violates the contract of an object, the object may throw this exception. For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception. Note that fail-fast behavior cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification.
Fail-fast operations throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: ConcurrentModificationException should be used only to detect bugs.
Since: 1. Method Summary Methods declared in class java. When possible, confine all references to a Collection object, so its is easier to prevent concurrent modifications. Make the Collection a private object or a local variable, and do not return references to the Collection or its iterators from methods. It is then much easier to examine all the places where the Collection can be modified. If the Collection is to be used by multiple threads, it is then practical to ensure that the threads access the Collection only with appropriate synchonization and locking.
I don't know if adding a version to all your persistent classes will sort it out, but that's one way that Hibernate can provide exclusive access to rows in a table. Could be that isolation level needs to be higher. If you allow "dirty reads", maybe you need to bump up to serializable. Note that the selected answer cannot be applied to your context directly before some modification, if you are trying to remove some entries from the map while iterating the map just like me.
I ran into this exception when try to remove x last items from list. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?
Collectives on Stack Overflow. Learn more. Asked 12 years, 8 months ago. Active 1 month ago. Viewed k times. Here is the full stack-trace: Exception in thread "poolthread-1" java.
ConcurrentModificationException at java. Improve this question. Raedwald Can you provide some more context? Are you merging, updating or deleting an entity? What associations doest this entity have? What about your cascading settings?
From the stack trace you can see that the Exception happens while iterating through the HashMap. Surely some other thread is modifying the map but the exception occurs in the thread that's iterating. Add a comment. Active Oldest Votes. Improve this answer. Robin Robin Possibly, but it looks as if Hibernate is doing the iterating, which should be implemented reasonably correctly. There could be callback modifying the map, but that is unlikely.
The unpredictability points to an actual concurrency problem. This exception has nothing to do with threading concurrency, it is caused by the backing store of the iterator being modified. Notice that this may only happen intermittently because we are calling remove inside a conditional statement. So the exception will be thrown whenever we have a String that starts with "A" in our list.
There are a number of ways to prevent ConcurrentModificationException, and we'll explore these below. We can change how we iterate by replacing the enhanced for-loop with a while loop that uses an Iterator object. The Iterator allows us to safely remove the matching element because we are not calling remove directly on the list object.
You can download all the example code from our GitHub Repository. Pankaj I love Open Source technologies and writing about my experience about them is my passion. Follow Author. Comments Arif says:. October 3, at am. Suhas says:. October 3, at pm.
Pankaj says:. October 8, at am. November 29, at am. October 5, at am. Abhishek Singh says:. May 26, at am. May 1, at am. Naveen Niraula says:. August 20, at pm. Dima says:. April 30, at pm. March 23, at am. April 16, at pm. December 15, at am. September 5, at am. Saurabh Gandhi says:. July 28, at am.
Ram Thota says:. May 4, at pm. V J reddy says:. October 8, at pm. Suma Gopalakrishna says:. July 22, at pm. April 14, at am. June 9, at pm. Thibault says:.
0コメント