Announcement Announcement Module
Collapse
No announcement yet.
Issue with Session Scoped Bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Issue with Session Scoped Bean

    Hello-

    I am using a session scoped bean in my web application, but it turns out these objects are not distinct across sessions. One user's object ends up being "used" by another. I have a "UserLog" object that has some member variables, public setters/getters, and a resetUserLog that just sets the member variables to null and -1s.

    Application Context
    Code:
    <bean id="newUserLog" class="org.nyumc.patientlog.domain.object.UserLog" scope="session">
    	<aop:scoped-proxy proxy-target-class="true"/>
    </bean>
    Controller
    This has been simplified, but not in a way that should matter for understanding what I am doing.
    Code:
    @Controller
    public class UserLogController {
    	
    	@Autowired
    	@Qualifier("newUserLog")
    	private UserLog newUserLog;
    	
    public String returnToLogPage(int pgnum, Model model){
    		newUserLog.setCurPage(pgnum);
    newUserLog = userLogManager.getNewUserLog(programId, formId, sessionManager.getUser(request).getId());
    	model.addAttribute("logPage", newUserLog.getPages().get(pgnum));
    	return "User/log";
    }
    
    @RequestMapping(value={"/createNewLog.html"})
    public String newLog(@RequestParam int programId, @RequestParam int formId, Model model, HttpServletRequest request){
    	newUserLog.resetUserLog();
    	return returnToLogPage(0, model);
    }
    
    @RequestMapping(value="/userLog.html", method=RequestMethod.POST)
    public String userLogSave(@ModelAttribute("logPage") UserLogPage logPage, Model model, HttpServletRequest request){
    	int goToPage = Integer.parseInt(request.getParameter("goToPage"));
    	for(int k = 0; k < logPage.getUserElements().size(); k++){		newUserLog.getPages().get(0).getUserElements().get(k).updateElementWithResponses(logPage.getUserElements().get(k));
    		}
    		if(newUserLog.getTotalNumPages() >= goToPage){
    			return returnToLogPage(goToPage, model);	
    		}else {
    			userLogManager.saveNewUserLog(newUserLog, sessionManager.getUser(request), false);
    			if(request.getParameter("createAnother") != null && request.getParameter("createAnother").equals("y")){
    				return returnToLogPage(1, model);
    			}
    			return "redirect:newLogHome.html?from=newlog";
    		}
    	}
    So, a user log can consist of multiple pages. I store the user log in the session so that this information entered remains from page to page, then I save the entire log at the end. If a user clicks a special "save and add another with this data" button [ see 'createAnother' above], I save the form, then show the first page of the log again with everything saved (because it should be saved in the newUserLog session scoped bean). Instead, when the user is brought to the first page of the new log, this first page is a DIFFERENT user's log, and therefore this newUserLog bean seems to be used across sessions. Why??

    Clea

  • #2
    You are destroying things yourself.

    Code:
    @Controller
    public class UserLogController {
    	
    	@Autowired
    	@Qualifier("newUserLog")
    	private UserLog newUserLog;
    	
    public String returnToLogPage(int pgnum, Model model){
    		newUserLog.setCurPage(pgnum);
    newUserLog = userLogManager.getNewUserLog(programId, formId, sessionManager.getUser(request).getId());
    	model.addAttribute("logPage", newUserLog.getPages().get(pgnum));
    	return "User/log";
    }
    
    @RequestMapping(value={"/createNewLog.html"})
    public String newLog(@RequestParam int programId, @RequestParam int formId, Model model, HttpServletRequest request){
    	newUserLog.resetUserLog();
    	return returnToLogPage(0, model);
    }
    
    @RequestMapping(value="/userLog.html", method=RequestMethod.POST)
    public String userLogSave(@ModelAttribute("logPage") UserLogPage logPage, Model model, HttpServletRequest request){
    	int goToPage = Integer.parseInt(request.getParameter("goToPage"));
    	for(int k = 0; k < logPage.getUserElements().size(); k++){		newUserLog.getPages().get(0).getUserElements().get(k).updateElementWithResponses(logPage.getUserElements().get(k));
    		}
    		if(newUserLog.getTotalNumPages() >= goToPage){
    			return returnToLogPage(goToPage, model);	
    		}else {
    			userLogManager.saveNewUserLog(newUserLog, sessionManager.getUser(request), false);
    			if(request.getParameter("createAnother") != null && request.getParameter("createAnother").equals("y")){
    				return returnToLogPage(1, model);
    			}
    			return "redirect:newLogHome.html?from=newlog";
    		}
    	}
    The highlighted code destroys the scoped proxy making it basically a singleton.. NEVER replace an instance variable...

    Comment


    • #3
      amazing! Stupid mistake on my part. Thanks so much for the speedy response.

      Comment

      Working...
      X