Howdy folks, I am trying to prevent multiple logins per user. I have done that and it works fine, but the code does not take into account synchronization of the code that adds and removes the username for each session. I am hoping for some help with this part. Also, if you notice anything that could improve the performance of the code, suggestions would be appreciated. Here is a link to the four classes that are involved. Code example There is: 1. MultipleLoginServlet that creates the HashMap upon starting the web container. 2. LoginHashMap that adds and removes the username value with the session ID as the key. 3. LoginServlet that adds the username to the LoginHashMap object when the user logs in. 4. LoginSessionListener that removes the username from the LoginHashMap when the session ends. Thanks, Howard
<a href="http://www.getlocaldeals.com" target="_blank" rel="nofollow">Free local coupons</a>
well, u can make the methods in LoginHashMap synchronized. And another suggestion, I guess it would be easy to set a flag in the user table in the database.It will cost you two database queries, one during login and logout each. This has an advantage that u can also monitor how many users are logged in by seeing the table. I guess this will not be possible with your code. And then there will not be any sync issues and moreover note that ur hashmap will grow as the number of users increase and you are maintaining this in your memory.... Well, opinions differ, u have the above mentioned two options. Shankar.
I'm in agreement with Shankar, I do not know the purpose of your application but if you might be dealing with an indefinite amount of users I would move your session management to a database. Also I would restructure how your database methods are being called. Since the database is considered a Model in the MVC Architecture, I would split all your database calls into another class. From within your Servlet (Controller) I would make a call out to your database. Your database class could be called something like UserSessionRetriever. If you do session management within a database you would have less synchronization to worry about, but you would still want your createSession method to be synchronized since there is a possibility for a user to have two windows open and logging in at the same time. Not saying it is probable, but it is likely. Breaking your Database into another class by following the MVC Architecture will save you time and frustration if you ever have to expand your servlet and database to do different processes. Just my $.02. Regards, Ryan
SCJP 1.4, SCWCD
Java: The power, elegance, and simplicity of a hand grenade
Joined: Jun 25, 2001
Thanks guys! I like the idea of using the database to track the sessions and I will create a separate class to make the db calls. Howard