This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Instance variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Instance variables" Watch "Instance variables" New topic
Author

Instance variables

Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
Regarding the multi thread issue in the previous discussion I understand that we shouldn't declare istance variables in the servlet.

But what about declaring instance variables in another class, which is called from the servlet? Please see the example below.

If two theads are requesting MyServlet at the same time, are there any risk that thread 2 could overwrite the public instance variables initiated in the class called SessionHandler, when called from the servlet as below?

Please note that a local instance of SessionHandler is created in the servlets doGet method.

Do you see any problems in the code below in an application with many requests?


Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

That looks fine (the principle - I haven't looked at the detail), because the instance of SessionHandler you're creating is local to the doGet method. As long as everything is local to the method then you're safe from threads interacting.


Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15959
    
  19

As long as the SessionHandler is an object created and destroyed within the scope of the doGet() method, there's no problem with SessionHandler and threads, because each thread has its own stack and a unique instance of SessionHandler will reside there. Since the difference between class members and instance members is that instance members are unique to each instance instead of being shared between instances like class members are, there's no conflict.


Customer surveys are for companies who didn't pay proper attention to begin with.
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
Tim Holloway wrote:As long as the SessionHandler is an object created and destroyed within the scope of the doGet() method, there's no problem with SessionHandler and threads, because each thread has its own stack and a unique instance of SessionHandler will reside there. Since the difference between class members and instance members is that instance members are unique to each instance instead of being shared between instances like class members are, there's no conflict.


So, do we also need to destroy the SessionHandler object manually, something like? If so, why is that?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Instance variables
 
Similar Threads
Could not resolve a persistence unit corresponding to the persistence-context-ref-name
Memory leak - Buffer size problem?
Synchronization problem with session?!
servlet mapping
JSP synchronization