Considerations for MongoDB Java connection pool

Source: Internet
Author: User

1. Mongo object

A connection pool is implemented internally. Mongo objects are thread-safe. Therefore, you can create only one Mongo object and use it safely in a multi-threaded environment. Therefore, we can use the Mongo variable as a member variable of the Singleton class to ensure that only one connection pool is created. The Mongo. close method will close all currently active connections. Make sure to call the close method when the web project is deregistered from the Tomcat or GlassFish container.

2. DB object

The DB object can be obtained through the Mongo. get method, representing a connection to the database. By default, after the database query or update operation is completed, the connection will automatically return to the connection pool. We do not need to manually call the code back to the pool. As for how to implement it, I guess there are finally blocks in the update, query, and save methods, and the code is also connected to the pool.

3. manually connect to the pool

The DB object can also perform multiple operations on a connection, such as the following code:

[Java]

  1. DB db ...;
  2. Db. requestStart ();
  3. Code ....
  4. Db. requestDone ();
RequestStart will invalidate the automatic connection to the pool. Therefore, ensure that requestDone can be called. The finally block should be more rigorous here.

4. Sample Code. The following class implements the Singleton mode of Lazy loading. The member variable Mongo mongo is instantiated only once. Pay attention to the connection pool size and reconnection settings.

[Java]

  1. PackageCom. freebird. helper;
  2. ImportCom. mongodb. Mongo;
  3. ImportCom. mongodb. MongoOptions;
  4. ImportCom. mongodb. DB;
  5. /** 
  6. * Describe class DBManager here. 
  7. * Example: 
  8. * Initialization: DBManager. getInstance (). init ("74.208.78.5", 27017,200 ); 
  9. * Then, each time the following code is used to obtain the database object 
  10. * DBManager. getInstance (). getDB (); 
  11. * Created: Sat Dec 17 10:45:24 2011 
  12. * @ Author <a href = "mailto: chenshu @ chunshu"> chenshu </a> 
  13. * @ Version 1.0 
  14. */
  15. Public ClassDBManager {
  16. Public Static FinalString DB_NAME ="Kaimei";
  17. Public Static FinalString MESSAGE_COLLECTION ="Email";
  18. Public StaticDBManager getInstance (){
  19. ReturnInnerHolder. INSTANCE;
  20. }
  21. /** 
  22. * Creates a new <code> DBManager </code> instance. 
  23. */
  24. PrivateDBManager (){
  25. }
  26. Private Static ClassInnerHolder {
  27. Static FinalDBManager INSTANCE =NewDBManager ();
  28. }
  29. PublicDB getDB (){
  30. ReturnMongo. getDB (DB_NAME );
  31. }
  32. PrivateMongo mongo;
  33. Public VoidInit (FinalString ip,IntPort,IntPoolSize)ThrowsJava.net. UnknownHostException {
  34. System. setProperty ("MONGO. POOLSIZE", String. valueOf (poolSize ));
  35. If(Mongo =Null){
  36. Mongo =NewMongo (ip, port );
  37. Optional options = mongo. getmediaoptions ();
  38. Options. autoConnectRetry =True;
  39. Options. connectionsPerHost = poolSize;
  40. }
  41. }
  42. }

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.