java实现mongodb的数据库连接池
程序员文章站
2024-03-07 23:48:33
mongodb是介于关系数据库和非关系数据库之间的一种产品,文件的存储格式为bson(一种json的扩展),这里就主要介绍java通过使用mongo-2.7.3.jar包实...
mongodb是介于关系数据库和非关系数据库之间的一种产品,文件的存储格式为bson(一种json的扩展),这里就主要介绍java通过使用mongo-2.7.3.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 * @description: 获取表tablename的链接dbcollection */ public dbcollection getdbcollection(string tablename) { return db.getcollection(tablename); } /** * @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)方法即可。
以上就是本文的全部内容,希望本文所述对大家学习java程序设计有所帮助。
上一篇: java中ZXing 生成、解析二维码图片的小示例
下一篇: java实现屏蔽词功能