Ankur
Ankur

Reputation: 51100

Best practice of managing database connections in a web app

I am developing a MongoDB app with Java but I think this question related to datastore connections for web apps in general.

I like to structure all web apps with four top-level packages which are called (which I think will be self explanatory):

Ideally I would like to have a class in the Dao package that handles all the connections details.

So far I have created a class that looks like this:

public class Dao {

public static Mongo mongo;
public static DB database;

public static DB getDB() throws UnknownHostException, MongoException{
    mongo = new Mongo("localhost");
    database = mongo.getDB("mydb");
    return database;
    }

public static void closeMongo(){
    mongo.close();
    } 
}

I use it in my code with something like this

public static void someMethod(String someData){
    try {
        DB db = Dao.getDB();
        DBCollection rColl = db.getCollection("mycollection");      
        // perform some database operations
        Dao.closeMongo();

    } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); 
    }
}

This seems to work fine, but I'd be curious to know what people think is the "best" way to handle this issue, if there is such a thing.

Upvotes: 3

Views: 3841

Answers (4)

Ankur
Ankur

Reputation: 51100

I got a 'nice' solution from this article. http://www.lennartkoopmann.net/post/722935345

Edit Since that link is dead, here's one from waybackmachine.org

http://web.archive.org/web/20120810083748/http://www.lennartkoopmann.net/post/722935345

Main Idea

What I found interesting was the use of a static synchronised method that returns an instance of the static class and its variables. Most professional devs probably find this obvious. I found this to be a useful pattern for managing the db connections.

Pooling

Mongo does automatic connection pooling so the key is to use just one connection to the datastore and let it handle its own pooling.

Upvotes: 1

Mithun Sasidharan
Mithun Sasidharan

Reputation: 20920

I would suggest you can write a java class to establish the connection with the database.

The arguments to the method should be the database name, password, host port and other necessary credentials.

You can always call the parametrized constructor everywhere where there is a need to establish database connectivity. This can be a model.

Upvotes: 1

Giovanni
Giovanni

Reputation: 4015

The rule of thumb when connecting to relational database server is to have a pool. For example if you connect to an oracle database using a pool gives you some performance benefits both in terms of connection setup time and sql parsing time (if you are using bind variables). Other relational database may vary but my opinion is that a pool is a good pattern even for some other reason (eg. you may want to limit the maximum number of connections with your db user). You are using MongoDB so the first thing to check is how MongoDB handles connections, how expnsive is creating a connection,etc. I suggest to use/build a class that can implements a pool logic because it gives you the flexibility you may need in the future. Looking at your code it seems that you api

DB db=Dao.getDB();

should be paired with:

Dao.closeDB(DB db);

So you have a chance to really close the connection or to reuse it without affecting the Dao code. with these two methods can switch the way you manage connections without recoding the Dao objects

Upvotes: 1

Krishan
Krishan

Reputation: 641

I think it is better if you call a method inside DAO to get data from database as well. If you do it in this way, say your database got changed. Then you have to edit many classes if you get data directly calling db queries. So if you separate db calling methods inside the DAO class itself and call that method to get data it is better.

Upvotes: 0

Related Questions