Re: ensuring unique object
lambelly wrote:
I guess, to phrase it better, I am looking for a way to make class
variables thread safe in tomcat.
On Feb 22, 1:14 pm, "lambelly" <lambe...@gmail.com> wrote:
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?
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.
--
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