MongoDB is a product between a relational database and a non-relational database. The file storage format is BSON (an extension of JSON). Here we mainly introduce Java to implement mongodb connection pool by using the mongo-2.7.3.jar package. The specific Java code implementation is as follows:
Database connection pool configuration parameters:
/** *@Description: mongo connection pool configuration file*/ package cn.lulei.mongo.pool; public class MongoConfig { private static String userName;//username private static String pwd;//password private static String[] host;//host address private static int[] port;//port address private static String dbName;//database name private static int connectionsPerHost = 20;//maximum number of connections per host private static int threadsAllowedToBlockForConnectionMultiplier = 10;//Number of thread queues private static boolean authentication = false;//Is authentication required 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; } } Database connection pool management class:
/** *@Description: mongo database connection pool management class*/ 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 * Instantiate dbName a 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()); } } /** * Instantiate using configuration parameters*/ public MongoManager() { this(MongoConfig.getDbName(), MongoConfig.getUserName(), MongoConfig.getPwd()); } /** * @param tableName * @return * @Description: Get the link to tableName DBCollection */ public DBCollection getDBCollection(String tableName) { return db.getCollection(tableName); } /** * @Description: mongo connection pool initialization*/ 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 { //Service List 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])); } //Connection pool parameter settings MongoOptions options = new MongoOptions(); options.connectionsPerHost = MongoConfig.getConnectionsPerHost(); options.threadsAllowedToBlockForConnectionMultiplier = MongoConfig.getThreadsAllowedToBlockForConnectionMultiplier(); mongo = new Mongo(replicaSetSeeds, options); //Readable from the server mongo.setReadPreference(ReadPreference.SECONDARY); } catch (Exception e){ e.printStackTrace(); } } } Let’s take a look at how to use this connection pool through a simple test class~
/** *@Description:mongo test*/ 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"); } }When using the above management class, you only need to initialize the MongoConfig class. For instances of MongoManager, you can use the MongoConfig configuration or set it by yourself. Each time you get DBCollection, you only need to call the getDBCollection (String tableName) method.
The above is all about this article. I hope that the description in this article will be helpful to everyone to learn Java programming.