We have a page which, after loading, makes an AJAX call to retrieve some more data which is iterated over in a forEach loop, i.e. the jsp downloaded by the ajax call contains a <c:forEach> tag. Before the page is rendered, the collection is cleared and recreated from the database. The collection referenced by the loop is supposed to be synchronized, but we have seen instances of ConcurrentModificationExceptions when the user repeatedly clicks the link to refresh the page. In the javadocs for Collections, it states that if you get a synchronized set, you need to put a synchronized block around any loops in java so that the list will be undisturbed while you iterate over it. Can this be done in JSTL/EL?
Easiest way to prevent concurrent access on it would be to have two different lists.
Could you change the logic so that rather than clearing and re-populating the list, it gets created fresh each time?
The old one would then be garbage collected once there were no more references to it, but still available for those currently using it (slightly out of date, but if you follow the advice from Paul/Bear and put the list in request scope then thats not a huge amount of time)
Joined: Dec 07, 2010
I think that that is ultimately what we will end up doing.
We have graph of objects that we store on the session relating to a particular user. We normally access this object in EL when we want to display something. For this particular browser request, the code refreshes some data cached on the session and then when the page renders, it accesses it in a forEach loop. Subsequent requests for the same resource on the same session sometimes lead to a race condition if the first request isn't finished.
I believe we are going to generate the collection as needed by the jsp now and throw it away when done.