Considerations for using MongoDB connection pooling in Java __java

Source: Internet
Author: User
Tags connection pooling finally block

A connection pool is implemented within the 1.Mongo object. The MONGO object is thread-safe, so you can only create one and use it securely in a multithreaded environment. Therefore, we can use the MONGO variable as a member variable of a singleton class to ensure that only one connection pool is created. The Mongo.close method closes all active connections currently in use. So be sure to call the Close method when the Web project is logged out of the Tomcat or GlassFish container.

2.DB objects

DB objects can be obtained through the Mongo.get method, representing a connection to the database. By default, the connection is automatically returned to the connection pool when the database query or update operation is completed. No need for us to manually call the code back into the pool. As for how to do that, I guess there's a finally block inside the Update,query,save method that has code connected to the pool.


3. Manually also connected into the pool

DB objects also allow you to perform multiple operations on a single connection, such as the following code:

 [Java] view plain copy print?   DB db ...;      Db.requeststart (); Code ... db.requestdone ();
Requeststart makes the automatic connection into the pool invalid, so you must ensure that the Requestdone can be invoked. The finally block should be more tightly used here.

4. Example code, the following class implements the singleton mode of lazy loading, where the member variable MONGO MONGO is instantiated only once. Note The connection pool size and reconnection settings.

[Java]  View plain copy print? package com.freebird.helper;      import com.mongodb.mongo;   Import  com.mongodb.MongoOptions;   import com.mongodb.db;     /**   *  Describe class DBManager here.   * example:   *  Initialization: Dbmanager.getinstance (). Init ("74.208.78.5", 27017,200);  after  * , each time the following code gets the database object    *  dbmanager.getinstance (). Getdb ();   * created: sat dec 17 10:45:24  2011   *   *  @author  <a href= "Mailto:chenshu@chunshu" >chenshu< /a>   *  @version  1.0   */   public class dbmanager {           public static final string db_name =   "Kaimei";          &NBSP;&NBSP;&NBSP;&NBSP;PUBLIC&NBsp;static final string message_collection =  "Email";           public static dbmanager getinstance () {        return innerholder.instance;       }           /**       * creates a new <code>dbmanager </code> instance.       *       */        private dbmanager ()  {              }          private static class  innerholder{       static final dbmanager instance = new  dbmanager ();       }          public  db getdb () {       return mongo.getdb (db_name);        }  

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.