Bruno Silva
Bruno Silva

Reputation: 618

Concurrent update and retrieving using spring MVC

I'm building an web application using Spring MVC+REST. I have some HTTP requests that update the state of the user and other that retrieve his state.

@RequestMapping(value = "/{id}", method = RequestMethod.POST)
@ResponseBody
public HttpEntity answer(@PathVariable(value = "id") Long id, @RequestParam("correct") Boolean result, Principal principal) throws JsonProcessingException, InterruptedException{
    Competence comp = competenceservice.findById(id);
    User user = userService.findByUsername(principal.getName());
    interactionService.registerInteraction(comp, result, user);
    return new ResponseEntity<>(HttpStatus.OK);
}

@RequestMapping(value = "/dailygoal/", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
public HttpEntity<DailyGoal> getDailyGoal(Principal principal) {
    Long goal = userservice.getDailyGoal(principal.getName());
    Long achieved = userservice.getGoalAchieved(principal.getName());
    return new ResponseEntity<>(new DailyGoal(goal, achieved), HttpStatus.OK);
}

I'm trying to keep them as independent as possible, but I'm having a problem: there are some situations when I make a request to update the user state and after that a retrieve his updated state (using another request), but, due to concurrency problems (I think), sometimes I get the state not updated. I've been thinking in two possible solutions:

Is there something important in one of the options that I have not considered? Does someone know a (better) third option?

Upvotes: 0

Views: 371

Answers (1)

Pulkit Gupta
Pulkit Gupta

Reputation: 1059

Your first approach is perfect. This is because you will always want the user to view the updated data as and when he is updating the data.

Consider this if I am updating a field I want to see the result or error in case there is some consistency issues so that I can take corrective action. I can only do this once update has returned with either a 200 OK a 4XX or a 5XX. Hence you should always wait for the update response and based on the response only decide what to do next.

Upvotes: 1

Related Questions