Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class Instance being overwritten by new Instance

 
Jason Allen
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written a class that store connection information for a user, such as UserID, Password, etc. This class is getting instanciated by my application at the point a user logs in. Everything works fine until another user logs in. Once this happens, the previous information is overwritten, as if using the same instance, instead of creating a new one. I am developing in JSP and am storing the class in the session variable.
Anyone have any thoughts on why this would happen or how to prevent it?
I am instanciating the class as follows:
db2Connect myConnection = new db2Connect();
I then do
session.setAttribute("connection", myConnection);
to store the connection object in my session.
I retrieve it using:
db2Connect myConnection = new db2Connect();
myConnection = (db2Connect) session.getAttribute("connection");
db2Connect is the class I wrote. The above is not copy/paste code, so ignore any syntax errors.
Thanks,
Jason.
 
Mark Savory
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your new user logging in using a new web browser?
[This message has been edited by Mark Savory (edited March 30, 2001).]
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason,
It looks like your session variable (setAttributte method) is behaving like a java.util.Properties object. The java.util.Properties object extends from a java.util.Hashtable which stores key/value pairs. The keys are unique and if you insert the same key with a different value, then the last insert clobbers in previous value with the same key.
Here are some suggestions to fix your problem.
1. Don't use the setAttribute method.
2. Create your own map collection using the UserID as the key.
-Peter
 
Jason Allen
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, they are logging on with IE 5.
 
Mark Savory
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I meant is: are the users logging in from separate browsers? I just wont to verify that there's a separate session object for each user.
 
Jason Allen
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, they are on another machine totally...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic