Java Operation MongoDB (connection pool)

Source: Internet
Author: User

Original link:Java operation MongoDB (connection pool)

The MONGO instance is actually a database connection pool, which has 10 links in the default connection pool. There is no need to re-implement this link pool, but we can change the configuration of this connection pool. Because the MONGO instance is a connection pool, it is best to have only one instance of MONGO in the project.

Common Configuration parameters:

connectionsperhost: Number of connections per host

threadsallowedtoblockforconnectionmultiplier: The number of thread queues, the result of which is multiplied by the above Connectionsperhost value is the maximum thread queue value. The "out of semaphores to get DB" error is thrown if the connection thread is fully queued.

maxwaittime: The maximum waiting time for a connected thread to block

connecttimeout: The milliseconds that the connection timed out. 0 is the default and unlimited

sockettimeout: The socket timed out. 0 is the default and unlimited

autoconnectretry: If this control is in a connection, the system will automatically retry

There are also many configurations that can be found in the MongoDB API.

Here's a look at the code:

[Java]View PlainCopyprint?
  1. Package Com.mongo.common;
  2. Import java.net.UnknownHostException;
  3. Import Com.mongodb.DB;
  4. Import Com.mongodb.Mongo;
  5. Import com.mongodb.MongoException;
  6. Import com.mongodb.MongoOptions;
  7. Public class Mongomanager {
  8. Private final static String HOST = "localhost"; Port
  9. Private final static int PORT = 27017; Port
  10. Private final static int poolsize = + ; Number of connections
  11. Private final static int BLOCKSIZE = 100; //wait Queue Length
  12. private static Mongo Mongo = null;
  13. Private Mongomanager () {}
  14. Static {
  15. Initdbprompties ();
  16. }
  17. public static DB Getdb (String dbName) {
  18. return Mongo.getdb (dbName);
  19. }
  20. /** 
  21. * Initialize Connection Pool
  22. */
  23. private static void Initdbprompties () {
  24. //Other parameters to be added according to the actual situation
  25. try {
  26. MONGO = new Mongoclient (HOST, PORT);
  27. Mongooptions opt = mongo.getmongooptions ();
  28. Opt.connectionsperhost = poolsize;
  29. Opt.threadsallowedtoblockforconnectionmultiplier = BLOCKSIZE;
  30. } catch (Unknownhostexception e) {
  31. } catch (Mongoexception e) {
  32. }
  33. }
  34. }


When used, as follows:

[Java]View PlainCopyprint?
  1. /**
  2. * Save
  3. *
  4. * @param user
  5. * @throws unknownhostexception
  6. */
  7. public void Save (user user) throws unknownhostexception {
  8. DB Mymongo = Mongomanager.getdb ("Mymongo");
  9. Dbcollection usercollection = mymongo.getcollection ("user");
  10. DBObject dbo = (dbobject) json.parse (User.tojson ());
  11. Usercollection.insert (dbo);
  12. }


Java Operation MongoDB (Connection pool) (RPM)

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.