ConcurrentModificationException on read/write from the cache

346 views
Skip to first unread message

Anoop Kaushik

unread,
Nov 15, 2017, 10:02:56 AM11/15/17
to ehcache-users
  1. What version of Ehcache you are currently using;3.4.0
  2. Paste the configuration for the Cache/CacheManager you have an issue with;
  3. Add any name and version of other library or framework you use Ehcache with (e.g. Hibernate); nothing other than JDK (tyrus.jar, javaee-api-7.0.jar
  4. Providing JDK and OS versions maybe useful as well. JDK1.8.0_121, Windows 7 Enterprise

I get the following eror (which is contrary to the reasons provided on this link - https://github.com/ehcache/ehcache3/issues/1188). The question is, if I start putting read-write locks on the cache to stop this rare occurence, the throughput of the reads will be heavily impacted.

My application opens web-socket connection with remote servers and streams security price data into Ehcache. The users connect to this server from UI and fetch the data from Ehcache to view it.

What do you recommend is the best way to handle this situation?

Exception in thread "Thread-1" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1437)
at java.util.HashMap$KeyIterator.next(HashMap.java:1461)
at org.ehcache.core.Ehcache.checkNonNullContent(Ehcache.java:612)
at org.ehcache.core.Ehcache.getAllInternal(Ehcache.java:308)
at org.ehcache.core.Ehcache.getAll(Ehcache.java:302)
at com.citadel.portal.cache.PortalCache.getAll(PortalCache.java:115)
at com.citadel.portal.cache.PortalCache.getAll(PortalCache.java:111)
at com.citadel.portal.data.portfolio.PortfolioUtil.getPortfolio(PortfolioUtil.java:87)
at com.citadel.portal.PortalMain$4.lambda$1(PortalMain.java:160)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at java.util.HashMap$ValueSpliterator.forEachRemaining(HashMap.java:1620)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at com.citadel.portal.PortalMain$4.run(PortalMain.java:162)
at java.lang.Thread.run(Thread.java:745)

Louis Jacomet

unread,
Nov 15, 2017, 10:26:43 AM11/15/17
to ehcach...@googlegroups.com
Hi,

The ConcurrentModificationException is caused by your code modifying the passed in Set of keys to the getAll method while Ehcache is using it.
You cannot do that. Even if we could handle you modifying this, the returned cache results would risk not taking into account the latest version of that set of keys to load.

Please make sure you no longer mutate the set of keys passed in getAll.

Regards,
Louis

--
You received this message because you are subscribed to the Google Groups "ehcache-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ehcache-user...@googlegroups.com.
To post to this group, send email to ehcach...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ehcache-users/00f7b1dc-ba31-4cae-b47d-be69232b75e8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anoop Kaushik

unread,
Nov 16, 2017, 3:56:52 PM11/16/17
to ehcache-users
I missed to notice that I maintain a one-to-many map to query for many entries at once from the cache. getAll reads using this one-to-many key-value. At the same time, other writer threads were adding new items in this one-to-many collection. Putting that behind read-write lock fixed the issue.

Thanks.
Reply all
Reply to author
Forward
0 new messages