Package gettasky.storage.mongodb

Source Code of gettasky.storage.mongodb.ConnectionFactory

package gettasky.storage.mongodb;

import gettasky.storage.StorageException;

import java.net.UnknownHostException;

import com.mongodb.Mongo;
import com.mongodb.DB;

/**
* Single point of access to MongoDB database connections.
*/
public class ConnectionFactory
{
    private static final String DATABASE_NAME = "gettasky";

    private static Mongo mongo;
   
    /**
     * Return a connection to the database.
     */
    public static DB getConnection() throws StorageException
    {
        try
        {
            return getMongoInstance().getDB(DATABASE_NAME);
        }
        catch (Exception e)
        {
            throw new StorageException(e);
        }
    }

    /**
     * Return the singleton instance of the Mongo connection pool.
     */
    private static Mongo getMongoInstance() throws UnknownHostException
    {
        if (mongo == null)
        {
            mongo = new Mongo(); // localhost and default port
        }
        return mongo;
    }
}
TOP

Related Classes of gettasky.storage.mongodb.ConnectionFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.