Take the 2-minute tour ×

I have seen an example of Singleton class object where it has a map containing user-id and password retrieved from database. This will help not to hit DB everytime during user login.

Now i am thinking, can't this be done through ServletContext attribute.

Basically on both the cases we will have application wide object.

So, Can we use ServletContext attribute in this case.

share|improve this question
Yes, a ServletContext attribute could be used. –  Paul Grime Feb 15 '12 at 16:56
Singletons are soo much overvalued, misunderstood and abused. Don't think about it until you really understand why you would need one. –  BalusC Feb 15 '12 at 18:16

2 Answers 2

up vote 2 down vote accepted

If you need to access the Map from anywhere in your application then yes it is actually the best place to place it.

You can register a Servlet Context Listener and on contextInitialized you can get a singleton and store it as an attribute of ServletContext

Then access it using by anywhere in your web application by getting the corresponding attribute


share|improve this answer

If the data in the DB is basically static, then yes, you could do so. If the data in the DB is dynamic, then no, this is not recommended. You'd otherwise need to write quite some code to keep the DB and the servletcontext variable in sync. When a new user registers on the webapp you of course want this user to be able to login.

Regardless, what if you have 1 million users in DB? Would you really want to copy this all into server's memory? For what purposes? This completely defeats the purpose of the DB. Do you want to validate a login? I really can't imagine why validating a login would be that expensive that you need to have a copy of the entire DB in server's memory. This is a job which a bit decent DB can do in less than a millisecond. The cause of the problem must definitely be solved elsewhere.

Perhaps you're unnecessarily copying the entire DB into Java's memory and doing the validation fully in Java on a per-row basis instead of utilizing the SQL WHERE clause? Something like this?

resultSet = statement.executeQuery("SELECT * FROM users");

while (resultSet.next()) {
    if (username.equals(resultSet.getString("username")) && password.equals(resultSet.getString("password"))) {
        valid = true;

This is a very often recurring thinking mistake among beginners who are new to databases and not understanding the powers of SQL. If you set an UNIQUE index on the username column and index the password column as well, the following is way much more efficient:

statement = connection.prepareStatement("SELECT id FROM users WHERE username=? AND password=md5(?)");
statement.setString(1, username);
statement.setString(2, password);
resultSet = statement.executeQuery();
valid = resultSet.next();
share|improve this answer
I am not sure I get your point completely.I mean is 1 M user names that many for memory?What if you use a caching mechanism for example?Do you think caching is a bad idea? –  Cratylus Feb 15 '12 at 20:27
@user384706: Point is, why would you have a copy of DB in memory if a well designed DB can pick the match for you in a subsecond? As to caching, I'd use JPA instead of JDBC. It allows second level cache configuration. –  BalusC Feb 15 '12 at 20:31
First of all, why are you mentioning copying the entire DB?The OP just wants the user-names cached.Let's assume that 1 M user do exist in the DB.Is that two much overhead to be placed e.g. in a HashMap?Additionally there are caching mechanisms for whole the DB; I think Guava cache does that and there is memcached and other libraries.Why do they exist if db is so fast?The reason I am asking these,is that you are more experienced than me and I am trying to get your point here. –  Cratylus Feb 15 '12 at 21:11

Your Answer


