Results 1 to 6 of 6

Thread: Thread safety in spring web application

  1. #1
    Join Date
    Sep 2010
    Posts
    6

    Default Thread safety in spring web application

    Hello,
    Im writting a web application using spring frame work where after successful login a dashboard is shown according to user's privilege.all the beans,controller,service & Dataacess are wired with default scope ie singleton. I have extended a abstract controller and code looks like this.

    Code:
    public ModelAndView handleRequestInternal(HttpServletRequest request,
                HttpServletResponse response) {
    
            Map<String, Object> map = new HashMap<String, Object>();
            
            HashMap userSession = (HashMap) request.getSession().getAttribute("userMap");
            String userPrivilege = userSession.get("privileges").toString();            
            int userId = StringUtilClass.convertToInt(userSession.get("userId").toString());
            
            HashMap<String, List> detailMap = dashboardService.getDetailsofUser(userId);        
            if (detailMap.containsKey("deptartment")) {
                map.put("deptartment", detailMap.get("deptartment"));
            }
            if (detailMap.containsKey("district")) {
                map.put("district", detailMap.get("district"));
            }    
    
            return (new ModelAndView("success", "map", map));
    
        }
    in the success.jsp, all the district and department under his privileges are displayed. how can i make this thread safe as multiple users will be loging in at same time .will there be any problem with thread saftey when i use unsynchronized objects like httpsession and HashMap. I have not synchronized the access of "map" in jsp also . do i need to synchrnize explicitly in all the beans and jsp to achieve thread safety ? Can any one guide me on this ?

    thanks
    Anuja
    Last edited by kajuna; Sep 29th, 2010 at 04:17 AM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    I suggest a forum search as this question has been answered numerous times before, I also suggest a read of java concurrency in practice this explains multithreading and thus thread safety in general and in detail.

    As long as you don't modify instance variables or store state in instance variables there is NO issue...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Sep 2010
    Posts
    6

    Default

    Thank you for the response. I have gone through the forum and from what i understood there should not be a synchronization problem as im not using any instance variable to hold user specific data. All are local variables and it is passed to jsp as attribute in request. But im bewildered to find wrong values ( values of other user's ) populated in combo when multiple users are logged in . This is not happening always. I thought it should be something related to thread safety.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    IF that is happening, there is something leaking state somewhere down your chain. The fact that your controller is thread safe doesn't mean all the other objects are threadsafe.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Sep 2010
    Posts
    6

    Default

    Im using scriplet in my jsp .
    Code:
    <%  Map mapGO =    (Map<String,List>)request.getAttribute("map");         
        HashMap userSessions  = (HashMap)request.getSession().getAttribute("userMap");
        String userPrivileges = userSessions.get("privileges").toString();    
    %>
    Will this cause some problem.

    Thanks
    anuja

  6. #6
    Join Date
    Sep 2010
    Posts
    6

    Default

    Hello all,
    problem still persists , could any one tell me whether i need to synchronize handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
    around session for multi user access. there are 100 users in the system who can view their respective district and department.
    isSynchronizeOnSession() in my controller returns false. could this be a problem ?
    Anuja

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •