I'm getting occasionally ConcurrentModificationException
with the following code:
public Set<MyObject> getTypes(Set<Type> names) {
Set<MyObject> myObjects = new HashSet<>();
myObjects = names.stream().filter(Objects::nonNull).mapToInt(Type::getId)
.mapToObj(cache::getMyObject)
.collect(Collectors.toSet());
I'm using cache to convert to MyObject
, but exception seems to thrown in collect
method (DAOImpl.java line 114)
java.util.ConcurrentModificationException
at java.util.HashMap$KeySpliterator.forEachRemaining(HashMap.java:1558)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at com.dao.DAOImpl.getTypes(DAOImpl.java:114)
at com.dao.DAOImpl$$FastClassBySpringCGLIB$$affe23c4.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:779)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:692)
at com.dao.DAOImpl$$EnhancerBySpringCGLIB$$6000bd7e.getTypes(<generated>)
How can I use cache to map to objects, or must I use cache outside stream?
Notice cache should be updated only 1 per day
The issue was that 2 threads changed myObjects
so the expected count of the Set
when looping through it was compromised, therefore the exception
The fix was to make the above service call atomic and therefore to prevent changing Set
on runtime with a different thread