• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Preventing multiple logins

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Howard Ralston
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any ideas?
 
Ranch Hand
Posts: 309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Howard Ralston:
Any ideas?


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.
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Howard Ralston
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic