Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Safety  RSS feed

 
Aijaz Mallick
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello,

As web server has multiple threads to serve client requests in Thread Pool & to ensure Thread Safety we should not use any variables or Objects at Instance/Class level.

But in case of Session Variable which one is the Best Practice as the Session object is used by all the requests to have the same Session ID.

My Code :
------------------

public class MyServlet extends HttpServlet {
private static Logger log = Logger.getLogger(ClientRegistrationServlet.class);

private HttpSession session; /* This is used at Instance Level*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
log.debug("ClientRegistrationServlet : doPost : Arrived in Servlet ");

String formNo = "";
session = request.getSession(false);

/* Or private HttpSession session; should be used here ..?? */
}
}

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The latter. What would be the point of storing the session at the instance level?
 
Aijaz Mallick
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So, does creating the session object at instance level risks Thread Safety..

And can you explain with an example that what will be the difference if i create Session Object at Instance Level or Locally in doPost() method.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The important thing is not where it is created, the important thing is where references to it are stored. (Also note that "request.getSession(false)" does not create a session, but that's besides the point here.)

I'm not sure what you want me to explain - you seem to understand already that servlet instance variables are not thread-safe without further safeguards. Servlet instances are used in multiple threads concurrently - so if you store something in an instance variable in one thread, it would be replaced by another concurrent thread, thus resulting in some of the threads accessing the wrong session objects.

But what I'm questioning is what purpose storing a reference to a session -which is user-specific- outside of the doGet/doPost methods can possibly have? Why do you think that might be a useful thing to do (even if it were safe to do, which it is not)?
 
Aijaz Mallick
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sorry but i didn't understood the last 2 lines .. What are you trying to ask..??
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm asking why you think that storing a reference to a session in an instance variable might be a useful thing to do.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!