1

When looping through a List that comes from a JPA query in WebLogic, the application throws a ConcurrentModificationException as soon as this list is traversed.

Caused By: java.util.ConcurrentModificationException
        at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
        at java.util.ArrayList$Itr.next(ArrayList.java:859)
(rest of stack trace omitted)

The pseudo-code looks as follows:

final List<SomeEntity> someEntities = someEntityDao.getAllMatchingSomeCriteria();
for (final SomeEntity someEntity : someEntities ) {
    // ...
}

It's important to note that the list someEntities is not modified in any way, shape or form by the code. Rather, I've come to the conclusion that WebLogic is still filling up the list in a separate thread, while the main thread is already traversing it.

It can be solved as follows:

final List<SomeEntity> someEntities = new ArrayList<>(someEntityDao.getAllMatchingSomeCriteria());
for (final SomeEntity someEntity : someEntities ) {
    // ...
}

But that means having to go through all of the code, and that's a last resort. Is there a way to tell WebLogic to stop trying to be clever?

SeverityOne
  • 2,476
  • 12
  • 25

1 Answers1

0

It may not be possible to iterate over a collection that is simultaneously changing independent of the iterator.

Thiyanesh
  • 2,360
  • 1
  • 4
  • 11
  • But that would still involve changing any code that loops through a list. – SeverityOne Feb 01 '21 at 10:53
  • @SeverityOne, True. Maybe give it a try annotating the related entities(relationship) as `FetchType.EAGER`. This will help in eager fetching related entries, but given that its a concurrent modification exception, i am not sure whether this will help. Please do update if you find a working solution. – Thiyanesh Feb 01 '21 at 18:13