0

I have a Javascript which sends multiple ajax requests at the same time. These requests are loading data which is stored in another object that is stored in the session. My problem is that the different requests overwrite each other loaded data because they can not share the loaded data.

I have altered the real code to make it more understandable. The Traincontainer has multiple Train-objects with an unique trainNr. Then the persons for that train get loaded and stored in that object. After that the container ist written back to the session.

What is the best approach to make this thread safe?

@GET @Path(value = "trains/{trainNr}/persons")
@Produces({MediaType.APPLICATION_JSON})
public List<Person> loadPersons(@Context HttpServletRequest request, @PathParam("trainNr") String trainNr) {
    // load data
    TrainContainer trainContainer = request.getSession().getAttribute("trainContainer");
    List<Person> persons = trainService.loadPersons(trainContainer, trainNr);

    // update session attribute
    request.getSession().setAttribute("trainContainer", trainContainer);

    return persons;
}

UPDATE: The Train object inside the trainContainer gets altered in loadPersons thats the reason to store the container back to the session. the different calls overwrite the loaded persons because they dont know of each other and at the end only the persons from the last requests are stored in the session

visualization

0riginal
  • 137
  • 1
  • 11
  • What would you expect concurrent access _to do_? Why do you need to get the `trainContainer` from the `Session` then set is back if it's the same object instance? I don't really understand the intent of this code... – Boris the Spider Nov 21 '16 at 15:05
  • they should know about the loaded persons from the other requests. At the end all persons should be stored in the object and not just from the last one – 0riginal Nov 21 '16 at 15:06
  • Why multiple requests to the same controller/resource without being the previous one processed ? – Vasu Nov 21 '16 at 15:06
  • @javaguy serverside programming is inherently concurrent. – Boris the Spider Nov 21 '16 at 15:07
  • because the gui must update immediately – 0riginal Nov 21 '16 at 15:07
  • @Boris what I mean why multiple requests from the SAME CLIENT without being the first one processed ? – Vasu Nov 21 '16 at 15:11
  • @javaguy in the simplest case, the user might refresh their browser. They might double click a button. They might have two windows open and hit submit on both. You cannot guarantee the behaviour of a client you do not control; so you should always assume the worst... – Boris the Spider Nov 21 '16 at 15:12
  • @Boris Now your point makes sense – Vasu Nov 21 '16 at 15:13
  • The concurrent requests are from the same user and this is needed because the persons need to be loaded for multiple trains at the same time – 0riginal Nov 21 '16 at 15:13
  • The simple solution is don't store it in session, so that you don't get into the problems of critical sections – Vasu Nov 21 '16 at 15:14
  • @OP I don't understand the logic. Why are you storing data that is inherently request specific, like the selected train, in the session - which is session specific? Why not use a [request attribute](http://stackoverflow.com/a/15433931/2071828)? – Boris the Spider Nov 21 '16 at 15:16
  • @BoristheSpider because it is used later – 0riginal Nov 21 '16 at 15:17
  • the result is returned as json to display with javascript but we need the data again in a later request Until now these requests were executet synchronously but that is making the loading too slow – 0riginal Nov 21 '16 at 15:18
  • What happens when the 2nd request comes from the UI, does it override the values of the previous request (if it is in the middle of processing) ? Or do you need to say that previous request is in progress... what is the requirement ? – Vasu Nov 21 '16 at 15:24
  • @javaguy This object is not an Entity – 0riginal Nov 21 '16 at 15:25
  • @javaguy yes it overwrites the values because the request parameter has null for the person list for the other trains – 0riginal Nov 21 '16 at 15:26
  • @javaguy i added a little visualization of the problem. only the persons from the last request are stored because this request has no access to the current session. Or is it possible to make a synchronyzed block and get the current session within the mthod? – 0riginal Nov 21 '16 at 15:37
  • @javaguy I think i have to store the loaded data in a separate variable and save the data on the last request in the session. It's ugly but maybe the fastest solution – 0riginal Nov 21 '16 at 15:49

0 Answers0