wood burning stoves 2.0*
The moose likes JSP and the fly likes Can you prevent another thread from modifying a collection while you iterate over it in EL? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Can you prevent another thread from modifying a collection while you iterate over it in EL?" Watch "Can you prevent another thread from modifying a collection while you iterate over it in EL?" New topic
Author

Can you prevent another thread from modifying a collection while you iterate over it in EL?

Rob Dennett
Ranch Hand

Joined: Dec 07, 2010
Posts: 81
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?

Thanks,
Rob
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

Where is the collection being stored? App scope? Session scope?

If it's in request scope, it's almost impossible for another thread to get a reference to the collection.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rob Dennett
Ranch Hand

Joined: Dec 07, 2010
Posts: 81
Session scope.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Session scope? Since you said
Before the page is rendered, the collection is cleared and recreated from the database.

it seems pretty clear that request scope would be the appropriate place for it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

Ah, is it necessary for it to be there? That means that any other thread running in the session can affect it, so unless it absolutely needs to be in the session, I'd move it to request scope.

It's also pretty rare for read/write stuff to be sent to a JSP. Is there any reasons that multiple threads need to modify this particular list?

[Edit: Paul seems to have made it a life goal to post 20 seconds before I do. ]
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1027
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)


Rob Dennett
Ranch Hand

Joined: Dec 07, 2010
Posts: 81
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.

Thanks,
Rob
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

That would be the approach I'd use. I almost always consider anything that's going to be consumed by a JSP as immutable and disposable.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can you prevent another thread from modifying a collection while you iterate over it in EL?