Java implementation MongoDB database connection pool _java

Source: Internet
Author: User
Tags connection pooling mongodb

MongoDB is a product between relational and non relational databases, and the file is stored in a Bson format (an extension of JSON), Here is the main description of Java by using the Mongo-2.7.3.jar package to implement MONGODB connection pool, 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;//user name private static string pwd;//password private static Str Ing[] host;//host address private static int[] port;//port address private static String dbname;//database name private static int connection Sperhost = 20;//Maximum number of connections per host private static int threadsallowedtoblockforconnectionmultiplier = 10;//thread queue number private static 
 Boolean authentication = false;//If authentication public static String GetUserName () {return userName is required; 
 The public static void Setusername (String userName) {mongoconfig.username = UserName; 
 public static String Getpwd () {return pwd; 
 The public static void SetPwd (String pwd) {mongoconfig.pwd = pwd; 
 public static string[] GetHost () {return host; 
 The 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; 
 The public static void Setdbname (String dbname) {mongoconfig.dbname = dbname; 
 public static int Getconnectionsperhost () {return connectionsperhost; public static void Setconnectionsperhost (int connectionsperhost) {mongoconfig.connectionsperhost = Connectionsperh 
 Ost public static int Getthreadsallowedtoblockforconnectionmultiplier () {return threadsallowedtoblockforconnectionmult 
 Iplier; public static void Setthreadsallowedtoblockforconnectionmultiplier (int threadsallowedtoblockforconnectionmultipli 
 ER) {mongoconfig.threadsallowedtoblockforconnectionmultiplier = Threadsallowedtoblockforconnectionmultiplier; 
 public static Boolean isauthentication () {return authentication; 
 The public static void Setauthentication (Boolean authentication) {mongoconfig.authentication = authentication; 
 } 
}

Database connection Pooling Management class:

 /** * @Description: MONGO database connection Pool Management category * * 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 * instantiated dbname a db/Public Mongomanager (String Dbnam E, 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 ()); /** * uses configuration parameters to instantiate/public Mongomanager () {This (Mongoconfig.getdbname (), Mongoconfig.getusername (), 
 Mongoconfig.getpwd ()); /** * @param tablename * @return * @Description: Get table tablename link dbcollection/public dbcollection g 
 Etdbcollection (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" lengt 
  H 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 setting mongooptions options = new Mongooptions (); 
   Options.connectionsperhost = Mongoconfig.getconnectionsperhost (); Options.threadsallowedtoblockforconnectionmultiplier = 
   Mongoconfig.getthreadsallowedtoblockforconnectionmultiplier (); 
   MONGO = new MONGO (replicasetseeds, options); 
  Readable mongo.setreadpreference from the server (readpreference.secondary); 
  catch (Exception e) {e.printstacktrace (); 
 } 
 } 
}

Here's a simple test class to see how to use this connection pool ~

 /** 
 * @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 you use the above management class, you only need to initialize the Mongoconfig class. Instances of class Mongomanager can be set either by using Mongoconfig configuration or by parameters, and each time a dbcollection is fetched, only the getdbcollection (String tablename) method is required.

The above is the entire content of this article, I hope this article will help you learn Java programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.