| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
10年前发布

基于java实现mongodb的数据库连接池

MongoDB是介于关系数据库和非关系数据库之间的一种产品,文件的存储格式为BSON(一种JSON的扩展)

这里就主要介绍一样在Java中通过使用mongo-2.7.3.jar这个jar包实现mongodb连接池的实现,具体的java代码实现如下:

数据库连接池配置参数:

 /**     *@Description:  mongo连接池配置文件   */   package cn.lulei.mongo.pool;        public class MongoConfig {      private static String userName;//用户名   private static String pwd;//密码   private static String[] host;//主机地址   private static int[] port;//端口地址   private static String dbName;//数据库名   private static int connectionsPerHost = 20;//每台主机最大连接数   private static int threadsAllowedToBlockForConnectionMultiplier = 10;//线程队列数   private static boolean authentication = false;//是否需要身份验证      public static String getUserName() {    return userName;   }   public static void setUserName(String userName) {    MongoConfig.userName = userName;   }   public static String getPwd() {    return pwd;   }   public static void setPwd(String pwd) {    MongoConfig.pwd = pwd;   }   public static String[] getHost() {    return host;   }   public static void setHost(String[] host) {    MongoConfig.host = host;   }   public static int[] getPort() {    return port;   }   public static void setPort(int[] port) {    MongoConfig.port = port;   }   public static String getDbName() {    return dbName;   }   public static void setDbName(String dbName) {    MongoConfig.dbName = dbName;   }   public static int getConnectionsPerHost() {    return connectionsPerHost;   }   public static void setConnectionsPerHost(int connectionsPerHost) {    MongoConfig.connectionsPerHost = connectionsPerHost;   }   public static int getThreadsAllowedToBlockForConnectionMultiplier() {    return threadsAllowedToBlockForConnectionMultiplier;   }   public static void setThreadsAllowedToBlockForConnectionMultiplier(     int threadsAllowedToBlockForConnectionMultiplier) {    MongoConfig.threadsAllowedToBlockForConnectionMultiplier = threadsAllowedToBlockForConnectionMultiplier;   }   public static boolean isAuthentication() {    return authentication;   }   public static void setAuthentication(boolean authentication) {    MongoConfig.authentication = authentication;   }  }

数据库连接池管理类:

 /**     *@Description: mongo数据库连接池管理类   */   package cn.lulei.mongo.pool;      import java.util.ArrayList;  import java.util.List;    import com.mongodb.DB;  import com.mongodb.DBCollection;  import com.mongodb.Mongo;  import com.mongodb.MongoOptions;  import com.mongodb.ReadPreference;  import com.mongodb.ServerAddress;      public class MongoManager {      private static Mongo mongo;   private DB db;      static{    init();   }      /**   * @param dbName   * @param userName   * @param pwd   * 实例化dbName一个DB    */   public MongoManager(String dbName, String userName, String pwd) {    if (dbName == null || "".equals(dbName)) {     throw new NumberFormatException("dbName is null");    }    db = mongo.getDB(dbName);    if(MongoConfig.isAuthentication() && !db.isAuthenticated()){     if (userName == null || "".equals(userName)) {      throw new NumberFormatException("userName is null");     }     if (pwd == null || "".equals(pwd)) {      throw new NumberFormatException("pwd is null");     }     db.authenticate(userName, pwd.toCharArray());    }   }      /**    * 使用配置参数实例化    */   public MongoManager() {    this(MongoConfig.getDbName(), MongoConfig.getUserName(), MongoConfig.getPwd());   }      /**    * @param tableName    * @return    * @Date:2014-3-19      * @Author:lulei      * @Description: 获取表tableName的链接DBCollection    */   public DBCollection getDBCollection(String tableName) {    return db.getCollection(tableName);   }      /**    * @Date:2014-3-19      * @Author:lulei      * @Description: mongo连接池初始化    */   private static void init() {    if (MongoConfig.getHost() == null || MongoConfig.getHost().length == 0) {     throw new NumberFormatException("host is null");    }    if (MongoConfig.getPort() == null || MongoConfig.getPort().length == 0) {     throw new NumberFormatException("port is null");    }    if (MongoConfig.getHost().length != MongoConfig.getPort().length) {     throw new NumberFormatException("host's length is not equals port's length");    }    try {     //服务列表     List<ServerAddress> replicaSetSeeds = new ArrayList<ServerAddress>();     for (int i = 0; i < MongoConfig.getHost().length; i++) {      replicaSetSeeds.add(new ServerAddress(MongoConfig.getHost()[i], MongoConfig.getPort()[i]));     }     //连接池参数设置     MongoOptions options = new MongoOptions();     options.connectionsPerHost = MongoConfig.getConnectionsPerHost();     options.threadsAllowedToBlockForConnectionMultiplier = MongoConfig.getThreadsAllowedToBlockForConnectionMultiplier();     mongo = new Mongo(replicaSetSeeds, options);     //从服务器可读     mongo.setReadPreference(ReadPreference.SECONDARY);    } catch (Exception e){     e.printStackTrace();    }   }  }

下面通过一个简单的测试类,来看下如何使用这个连接池~

 /**     *@Description:   mongo测试   */   package cn.lulei.mongo.test;      import cn.lulei.mongo.pool.MongoConfig;  import cn.lulei.mongo.pool.MongoManager;      public class Test {     public static void main(String[] args) {    // TODO Auto-generated method stub     String[] host = {"127.0.0.1"};    int[] port = {27001};    MongoConfig.setHost(host);    MongoConfig.setPort(port);    MongoConfig.setDbName("novel");    MongoManager mongoManager = new MongoManager();    mongoManager.getDBCollection("chapter");   }    }

在使用上述管理类时,只需要初始化MongoConfig类即可。对类 MongoManager的实例话,既可以使用MongoConfig的配置也可以自己通过参数来设置,每次获取DBCollection 时,只需要调用 getDBCollection(String tableName)方法即可

出处: http://blog.csdn.net/xiaojimanman/article/details/22652239