mongodb数据库连接池(java版)

package storm.db;


import java.util.ArrayList;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

/**
* 类名: MongoDBDao
* 作者:lky
*/
public interface MongoDBDao {
    /**
     * 
     * 方法名:getDb
     * 作者:lky
     * 描述:获取指定的mongodb数据库
     * @param dbName
     * @return
     */
    public DB getDb(String dbName);
    /**
     * 
     * 方法名:getCollection
     * 作者:lky
     * 描述:获取指定mongodb数据库的collection集合
     * @param dbName    数据库名
     * @param collectionName    数据库集合名
     * @return
     */
    public DBCollection getCollection(String dbName, String collectionName);
    /**
     * 
     * 方法名:inSert
     * 作者:lky
     * 描述:向指定的数据库中添加给定的keys和相应的values
     * @param dbName
     * @param collectionName
     * @param keys
     * @param values
     * @return
     */
    public boolean inSert(String dbName, String collectionName, String keys, Object values);
    /**
     * 
     * 方法名:delete
     * 作者:lky
     * 描述:删除数据库dbName中,指定keys和相应values的值
     * @param dbName
     * @param collectionName
     * @param keys
     * @param values
     * @return
     */
    public boolean delete(String dbName, String collectionName, String keys, Object values);
    /**
     * 
     * 方法名:find
     * 作者:lky
     * 描述:从数据库dbName中取出相应数目的数据
     * @param dbName
     * @param collectionName
     * @param keys
     * @param values
     * @param num
     * @return
     */
    public ArrayList<DBObject> find(String dbName, String collectionName, int num);
    /**
     * 
     * 方法名:update
     * 作者:lky
     * 描述:更新数据库dbName,用指定的newValue更新oldValue
     * @param dbName
     * @param collectionName
     * @param oldValue
     * @param newValue
     * @return
     */
    public boolean update(String dbName, String collectionName, DBObject oldValue, DBObject newValue);
    /**
     * 
     * 方法名:isExit
     * 作者:lky
     * 描述:判断给定的keys和相应的values在指定的dbName的collectionName集合中是否存在
     * @param dbName
     * @param collectionName
     * @param keys
     * @param values
     * @return
     */
    public boolean isExit(String dbName, String collectionName, String key, Object value);
}
  • 数据库连接池的实现
package storm.db;

import java.net.UnknownHostException;
import java.util.ArrayList;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;
import com.mongodb.WriteResult;

/*
 * mongodb数据库链接池
 */
public class MongoDBDaoImpl implements MongoDBDao
{
    private MongoClient mongoClient = null;
    private static final MongoDBDaoImpl mongoDBDaoImpl = new MongoDBDaoImpl();// 饿汉式单例模式

    private MongoDBDaoImpl()
    {
        if (mongoClient == null)
        {
            MongoClientOptions.Builder buide = new MongoClientOptions.Builder();
            buide.connectionsPerHost(100);// 与目标数据库可以建立的最大链接数
            buide.connectTimeout(1000 * 60 * 20);// 与数据库建立链接的超时时间
            buide.maxWaitTime(100 * 60 * 5);// 一个线程成功获取到一个可用数据库之前的最大等待时间
            buide.threadsAllowedToBlockForConnectionMultiplier(100);
            buide.maxConnectionIdleTime(0);
            buide.maxConnectionLifeTime(0);
            buide.socketTimeout(0);
            buide.socketKeepAlive(true);
            MongoClientOptions myOptions = buide.build();
            try
            {
                mongoClient = new MongoClient(new ServerAddress("127.0.0.1", 27017), myOptions);
            } catch (UnknownHostException e)
            {
                e.printStackTrace();
            }
        }
    }

    public static MongoDBDaoImpl getMongoDBDaoImpl()
    {
        return mongoDBDaoImpl;
    }

    @Override
    public DB getDb(String dbName)
    {
        return mongoClient.getDB(dbName);
    }

    @Override
    public DBCollection getCollection(String dbName, String collectionName)
    {
        DB db = mongoClient.getDB(dbName);
        return db.getCollection(collectionName);
    }

    @Override
    public boolean inSert(String dbName, String collectionName, String keys, Object values)
    {
        DB db = mongoClient.getDB(dbName);
        DBCollection dbCollection = db.getCollection(collectionName);
        long num = dbCollection.count();
        BasicDBObject doc = new BasicDBObject();
        doc.put(keys, values);
        dbCollection.insert(doc);
        if (dbCollection.count() - num > 0)
        {
            System.out.println("添加数据成功!!!");
            return true;
        }
        return false;
    }

    @Override
    public boolean delete(String dbName, String collectionName, String keys, Object values)
    {
        WriteResult writeResult = null;
        DB db = mongoClient.getDB(dbName);
        DBCollection dbCollection = db.getCollection(collectionName);
        BasicDBObject doc = new BasicDBObject();
        doc.put(keys, values);
        writeResult = dbCollection.remove(doc);
        if (writeResult.getN() > 0)
        {
            System.out.println("删除数据成功!!!!");
            return true;
        }
        return false;
    }

    @Override
    public ArrayList<DBObject> find(String dbName, String collectionName, int num)
    {
        int count=num;
        ArrayList<DBObject> list = new ArrayList<DBObject>();
        DB db = mongoClient.getDB(dbName);
        DBCollection dbCollection = db.getCollection(collectionName);
        DBCursor dbCursor = dbCollection.find();
        if (num == -1)
        {
            while (dbCursor.hasNext())
            {
                list.add(dbCursor.next());
            }
        } else
        {
            while(dbCursor.hasNext())
            {
                if(count==0) break;
                list.add(dbCursor.next());
                count--;
            }
        }
        return list;
    }

    @Override
    public boolean update(String dbName, String collectionName, DBObject oldValue, DBObject newValue)
    {
        WriteResult writeResult = null;
        DB db = mongoClient.getDB(dbName);
        DBCollection dbCollection = db.getCollection(collectionName);
        writeResult = dbCollection.update(oldValue, newValue);
        if (writeResult.getN() > 0)
        {
            System.out.println("数据更新成功");
            return true;
        }
        return false;
    }

    @Override
    public boolean isExit(String dbName, String collectionName, String key, Object value)
    {
        DB db = mongoClient.getDB(dbName);
        DBCollection dbCollection = db.getCollection(collectionName);
        BasicDBObject doc = new BasicDBObject();
        doc.put(key, value);
        if (dbCollection.count(doc) > 0)
        {
            return true;
        }
        return false;
    }
    public static void main(String args[])
    {
        MongoDBDaoImpl mongoDBDaoImpl=MongoDBDaoImpl.getMongoDBDaoImpl();
        ArrayList<DBObject> list=new ArrayList<DBObject>();
        list=mongoDBDaoImpl.find("jd", "phone",-1);
        System.out.println(list.size());
    }
}

注:数据库连接池的实现是用的单例模式(懒汉式)

posted @ 2015-09-03 18:56  奋斗的珞珞  阅读(13925)  评论(0编辑  收藏  举报