Search code examples
javamultithreadingservletsconcurrencyjava.util.concurrent

java.util.ConcurrentModificationException while adding elements?


I have a servlet which uses a service to parse a YAML file. but when I put some user traffic on my servlet I get:

SEVERE: Servlet.service() for servlet [SitesController] in context with path [] threw exception [java.util.ConcurrentModificationException] with root cause
java.util.ConcurrentModificationException
    at java.util.ArrayList$SubList.checkForComodification(ArrayList.java:1129)
    at java.util.ArrayList$SubList.listIterator(ArrayList.java:1009)
    ...
    at com.example.UrlRedirectEngin.redirectoToRespectiveSubDomain(UrlRedirectEngin.java:250)

I am not sure if the exception is from parsing the YAML file or not. However the exception is pointing to the line below which I render the page line 250:

request.getRequestDispatcher(JSP_PAGE).forward(request, response);

I just want to know if ConcurrentModificationException can also happens while adding elements to a Map? If yes how can I handle this issue. Thanks.


Solution

  • The line

    request.getRequestDispatcher(JSP_PAGE).forward(request, response);
    

    forwards to an JSP page. The error may be inside of that JSP as well (the part of the stacktrace may hide the original error)