We are not using a class variable like you have mentioned. However, we are
using
a SessionManager which runs as a seperate thread & is in charge of adding,
deleting & retrieving sessions.
Can there be a problem with the SessionManager thread & the servlet thread
accessing the session in the following code?
This is the code which creates the collection to store the sessions
public PCTSSessionManager(long timeOut) {
d_Sessions = Collections.synchronizedMap(new HashMap());
}
This is the code that retrieves a session for the servlet thread
public PCTSSession getSession(String userId) {
PCTSSession rv = (PCTSSession)d_Sessions.get(userId);
if (rv != null) {
validateSession(rv);
if (rv.isValidSession()) {
rv.touch();
}
}
return rv;
}
This is the code that adds a session to the collection maintained by the
SessionManager
public void addSession(PCTSSession se) {
// this would kill another session if the user is already there.
synchronized(d_Sessions) {
d_Sessions.put(se.getUserId(),se);
}
}
S Itzkowitz
[EMAIL PROTECTED]
Kevin Mukhar <[EMAIL PROTECTED]>@JAVA.SUN.COM>
Friday January 26, 2001 01:05 PM
Please respond to "A mailing list for discussion about Sun Microsystem's
Java Servlet API Technology." <[EMAIL PROTECTED]>
Sent by: "A mailing list for discussion about Sun Microsystem's Java
Servlet API Technology." <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED]
cc:
Subject: Re: Session Swapping Error
Susie Itzkowitz wrote:
>
> We are having a problem once in awhile such
> that the HttpServletResponse is being
> swapped between users. For example, a user is expecting a response to a
> request submitted & will receive a response of
> another user. Is it possible that there is some sort of threading
problem?
It certainly sounds like a threading problem. It probably has nothing to
do with the Request or Response or session tracking. Let me guess,
you're doing something like this:
public class SusieServlet extends HttpServlet {
private PrintWriter out;
public void doPost(HttpServletRequest req, HttpServletResponse) {
//get the session, do other processing..... then
response.setContentType("text/html");
out = response.getWriter();
//output the response
}
}
The above is not thread safe because it is using a member variable that
gets changed by every thread that enters the servlet. If two threads
enter the servlet at the same time, each thread will set the PrintWriter
variable. This is a common problem to the symptom you described. If you
are using variables which can be changed by a thread, you should change
to using local variables, or protect access to those variables.
K Mukhar
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html