This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Rob Dennett
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Dennett
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Session scope.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1720
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic