• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Session scope.
 
Marshal
Posts: 27450
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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. ]
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Would you like to try a free sample? Today we are featuring tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic