Results 1 to 5 of 5
  1. #1
    JKoder70014 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default Synchronization of ServletContext Object

    Hi All,

    -- I am trying to make the ServletContext synchronize because when 2 or more clients, are trying to modify the same servletcontext attributes, then that will be a problem. Because Servlet A tries to set context attribute as context.setAttribute("A","test1"); and Servlet B tries to set context attribute as context.setAttribute("A","B related code"); It could happen because Servlet B doesn't know that Servlet A also has the same attribute. That will be a problem as now servlet A will get the output as "B related Code" instead of "test1" if both acces the same code at a time, and Servlet B changes before servlet A recieves the response

    --If clientA write synchronize(getServletContext()) and in that he sets 2 attributes context.set("A","test1") and context.set("B", "test2"), clientA first gets the lock on the ServletContext object.
    --But another person (clientB) from another servlet also try to access the synchronize(getServletContext()) method and try to replace the values of the attributes "A" and "B" when clientA's lock is still there.
    Will clientB can access it...? I think no, because clientA has already lock
    .
    -- Now my question is, will clientB will able to run his Servlet or any other client can run their servlets, on the same context , as clientA has already a lock on contextobject. And for the servlet to run, the servlet should be on running on that context itself and since the lock has already been obtained for that context, how can other client requests be servered for anyting ( I meant not only for the attributes but any other code in it), until clientA releases the lock ?

    NOTE: I think that getServletContext() will return the same context reference always instead of creating with the "new" operator each time. i.e, singleton object.

    Please clarify me regarding this..!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default

    Why are you using the ServletContext to hold data like that?
    There is one per web app.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default

    Why are you using the ServletContext to hold data like that?
    There is one per web app.

  4. #4
    JKoder70014 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    I got through this scenario when I was making some R & D. So I am looking for the clarification, on how to proceed if we are in this type of scenario. Ofcourse, we can go with request, session or any properties file or database. But anyway it should be possible from the context also. That's the reason I am looking for some way..!

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default

    You don't use the ServletContext like this...which is why you'll have problems.

    You lock the Context then other requests coming in will block...you just single-threaded your application.

Similar Threads

  1. need help in locking or synchronization
    By samanyu in forum New To Java
    Replies: 24
    Last Post: 07-06-2011, 06:19 AM
  2. java RMI and synchronization
    By guest_user in forum New To Java
    Replies: 15
    Last Post: 07-05-2011, 12:28 AM
  3. Threads and Synchronization
    By ASADUN in forum Threads and Synchronization
    Replies: 4
    Last Post: 12-18-2009, 07:00 AM
  4. i need help for ServletContext
    By samiahmed in forum Java Servlet
    Replies: 2
    Last Post: 09-24-2008, 10:46 AM
  5. Animation Synchronization
    By dreadrocksean in forum Advanced Java
    Replies: 5
    Last Post: 08-08-2008, 02:56 AM

Posting Permissions

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