0

Currently I am using mongodb with java. I don't want to reopen my connection to the datastore every time, so I thought I should make a static variable:

package Config;

import java.net.UnknownHostException;

import com.google.code.morphia.*;

import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class Config {

    static String dbUrl = "url";
    static int dbPort = portnumber;
    static String username = "user";
    static String password = "pw";
    static String dbName = "dbname";
    public static Datastore ds;

    public static Datastore getDatastore() throws UnknownHostException,
            MongoException {
        if (ds == null) {
            ds = connect();
        }
        return ds;
    }

    public static Datastore connect() throws UnknownHostException,
            MongoException {
        Mongo m = new Mongo(dbUrl, dbPort);
        Datastore ds = new Morphia().createDatastore(m, dbName);
        boolean con = ds.getDB().authenticate(username, password.toCharArray());// todo
        return ds;
    }
}

I would use it like this:

Config.getDatastore().doSomthing();

Beside those un-handled exceptions, is this the correct way of using a static variable?

pb2q
  • 58,613
  • 19
  • 146
  • 147
Maik Klein
  • 15,548
  • 27
  • 101
  • 197
  • 1
    Googling around tells me the MongoDB Java driver pools connections internally: http://www.littlelostmanuals.com/2011/11/increasing-mongodb-connection-pool.html You can "reopen" them without losing performance. – millimoose Aug 02 '12 at 15:32
  • 1
    Also, if this is a webapp, then `getDataStore()` is glaringly non-thread-safe. You're not guaranteed that every request will get the same `DataSource` instance, the first few that hit the race condition can get several different ones. – millimoose Aug 02 '12 at 15:35
  • 1
    Use a singleton pattern : [Check here](http://en.wikipedia.org/wiki/Singleton_pattern) – Jérôme Boé Aug 02 '12 at 15:35

3 Answers3

4

Better would be making Config class as a singleton.

Santosh
  • 17,667
  • 4
  • 54
  • 79
2

The given Config seems to indicate that this class should be a singleton, and if so, then using static variables for all members is fine. Will you have multiple Configs, or only one?

If Config isn't intended to be a singleton, then static isn't required in this case, or if so, only if it's ok to share your Datastore with every instance of your Config class. You'll achieve your goals with a simple - non-static - member variable, and you can still have, e.g. a getDataStore method. But each Config class will have it's own Datastore

Only use static if you want to also share that Datastore among multiple instances of your Config class.

pb2q
  • 58,613
  • 19
  • 146
  • 147
  • Thanks, yes I want to share the config class to all my classes. They all should have the same datastore. I want to prevent multiple object creations like ´new Mongo´ and ´new Morphia´. I will have a look at singleton – Maik Klein Aug 02 '12 at 15:46
1

is this the correct way of using a static variable?

well, your code seems to compile so as per the language its correct. As far as semantics are concerned the usage depends. it depends on the class in questions, the design you want to achieve.

I would personally go for a connection pool. If several connections are not required, i may go for a singleton if i need to maintain only one connection. If a single connection is not required i would go with a member variable.

In your case, it would depend on how you want to use the config class. In general design terms, what you have done is problematic for following reasons:

  • Everything is static in your class, it serves no purpose in terms of object oriented-ness.
  • You can not unit test this class unless you use powermock or some other framework.
  • what happens when someone calls connect() several times?

*Note: Singleton is an anti-pattern. Generally any global state is bad. I would avoid it as much as i can.

Community
  • 1
  • 1
Ravi Bhatt
  • 3,147
  • 19
  • 21