Just getting started with MongoDB, when doing an application, is affected by the previously used relational database, which takes into account the database connection pool issues.
relational database, we do the connection pool is simply to establish a good n connection (connection), and built into a connection pool (connection pool), provide to connect and return the connection and other operations.
In MongoDB, let's take a look at how to do this, take insert as an example:
Mongo m = new Mongo ("localhost", 27017); DB db = M.getdb ("MyDB"); Get collection Dbcollection coll = db.getcollection ("testcollection")//insert basicdbobject doc = new Basicdbobject ( ); ... Coll.insert (DOC); [Pseudo code] |
If you apply the previous experience, you may want to bias, quoting the official document of a sentence may be enlightened.
Note: The Mongo object instance actually represents a pool of connectionsto the database; You'll only have need one object of class Mongo even with multiple threads. See the Concurrency Doc page for more information.
The Mongo class is designed to be thread safe and shared among threads. Typically you create only 1 instance for a given DB cluster and use it across your app. If for some reason your decide to create many MONGO intances, note that:all Resource usage limits (max connections, etc) a Pply per MONGO instance to dispose of a instance, make sure-call Mongo.close () to clean up resources
The MONGO instance is already a ready-made connection pool and thread-safe. This built-in connection pool defaults to 10 connections, each of which takes a connection, and then releases the connection after performing the operation.
Two. Important parameters of the connection pool
The built-in connection pool has several important parameters, namely: connectionsperhost: Number of connections per host Threadsallowedtoblockforconnectionmultiplier: Number of thread queues, The result of multiplying the Connectionsperhost value above 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 number of threads waiting for a connection to block time ConnectTimeout: The milliseconds the connection timed out. 0 is the default and infinite Sockettimeout:socket timeouts. 0 is the default and infinite Autoconnectretry: If this control is in a connection, the system will automatically retry
It is set up in the following ways:
Mongooptions opt = mongo.getmongooptions (); Opt.connectionsperhost = 10; Poolsize opt.threadsallowedtoblockforconnectionmultiplier = 10; Other parameters are similar |
For details, refer to the MongoDB API:
Field Summary |
Boolean |
Autoconnectretry If true, the driver would keep trying to connect to the same server in case of the socket cannot be established. |
Int |
Connectionsperhost The maximum number of connections allowed per host for this Mongo instance. |
Int |
ConnectTimeout The connection timeout in milliseconds. |
Dbdecoderfactory |
dbdecoderfactory Override the Dbcallback factory. |
Dbencoderfactory |
dbencoderfactory Override the encoding factory. |
String |
Description The description for Mongo instances created with these options. |
Boolean |
Fsync |