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); }