Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2007

Tip: Looking for answers? Try searching our database.

ensuring unique object

Thread view: 
lambelly - 22 Feb 2007 18:14 GMT
I have created an object that is used in a servlet on tomcat. It is
very important that this object and all of the information it contains
be unique to each request obtained by tomcat. However, this doesn't
appear to be the case.

When I create the object I say:
Client = new Client();

and the object is instantiated and all of it's variables initialized
in the contructor. Past that point I use setters and getters to set
information in the object. The only place I don't use a setter or a
getter is when I call an authenticate() method in the client object
that doesn't return a value. Instead, it gets a security key in a web
service and sets that value in the client itself.

Is it possible that values that are being set in one instance of the
servlet and the object are affecting other instances of the servlet
and object? How do I stop that from occurring?
lambelly - 22 Feb 2007 20:03 GMT
I guess, to phrase it better, I am looking for a way to make class
variables thread safe in tomcat.

> I have created an object that is used in a servlet on tomcat. It is
> very important that this object and all of the information it contains
[quoted text clipped - 14 lines]
> servlet and the object are affecting other instances of the servlet
> and object? How do I stop that from occurring?
Tor Iver Wilhelmsen - 22 Feb 2007 21:38 GMT
På Thu, 22 Feb 2007 21:03:03 +0100, skrev lambelly <lambelly@gmail.com>:

> I guess, to phrase it better, I am looking for a way to make class
> variables thread safe in tomcat.

Use a ThreadLocal, or even better in the web application case, use request  
attributes.

So, either

// in the servlet
static final Threadlocal clientHolder = new ThreadLocal();

// in the request code

clientHolder.put(new Client());

// For retrieval

Client threadsClient = clientHolder.get();

Or

request.setAttribute(CLIENT_ATTR_NAME, new Client());

Client requestClient = (Client) request.getAttribute(CLIENT_ATTR_NAME);
Nigel Wade - 23 Feb 2007 10:51 GMT
> I guess, to phrase it better, I am looking for a way to make class
> variables thread safe in tomcat.

>> I have created an object that is used in a servlet on tomcat. It is
>> very important that this object and all of the information it contains
[quoted text clipped - 14 lines]
>> servlet and the object are affecting other instances of the servlet
>> and object?

Yes, if they are class variables.

>> How do I stop that from occurring?

The simplest answer is to make them instance variables. Class variables are
shared between every object of that class. Besides being shared, in a
multi-threaded servlet container all access to those variables should be
synchronized.

What values are you talking about? Are these part of the class Client, or part
of the servlet? If they are part of the servlet the same rule applies, class
variables for the servlet will be shared between different servlet instances.

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555

Joe Schlobotnick - 25 Feb 2007 20:18 GMT
<snip>
> If they are part of the servlet the same rule applies, class
> variables for the servlet will be shared between different servlet instances.

While this is true, you must beware that Servlet instances are reused by
multiple HTTP requests.  So even though a variable is defined as an
instance variable in the Servlet, it can be helpful to treat them like
class variables that are shared by multiple instances.

I've seen this in practice:  a servlet with an instance variable
indicating the language in which the user wants to see the page, where
that language variable is used in SQL queries to retrieve content from
the database in the appropriate language.  Under high load, a user could
see the page switch languages half way down as another user request
started being serviced by the same servlet instance and switched the
"instance" variable's value.

..Joe


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.