I have a button, which calls ajax POST method on a predefined URL. After the server has done its thing, it simply refreshes the page
The server fetches User object from the database and updates one column.
When the page refreshes then about 50% of the time, the User is still not updated and the refreshed page displays old information
Note - Code is cleaned up to only show the relevant
Ajax post method:
function buttonClick(){
$.ajax({
type : "POST",
url : "?open_class=1",
success : refresh()
});
}
function refresh(){
window.location = contextPath;
}
Spring MVC
@RequestMapping(value = "/manager/listofquestions", method = RequestMethod.POST)
public String listOfQuestionsPost(
Model model,
HttpServletRequest request,
Principal principal) {
if(request.getParameter("open_class") != null){
userService.openClass(principal.getName());
}
return "success";
}
UserService
@Transactional
public void openClass(String name) {
User user = userDao.getByName(name);
user.setCodeword(codeWordService.GetNewCodeWord());
userDao.update(user);
}
UserDao
public User update(User user) {
sessionFactory.getCurrentSession().update(user);
return user;
}
public User getByName(String userName) {
Query query = sessionFactory.getCurrentSession().createQuery("from User where username=?");
query.setString(0, userName);
query.setCacheable(true);
Object[] list = query.list().toArray();
if(list.length == 0) {
return null;
}
return (User) list[0];
}