Due to business needs, the company recently transferred part of the data to the MongoDB database.
After deploying the MongoDB database with the corporate DBA, we started our Java Connection MongoDB practice
Before you start, recommend two IDE tools to connect to your database
1.rockmongo-on-windows-v0.0.2 (only run successfully in Firefox browser)
2. Robomongo (in 32 machine XP system will not start)
Mongomanager Function (single case mode to implement MONGO)
public class Mongomanager {
private static final Logger Logger = Logger.getlogger (mongomanager.class);
private static Mongo Mongo = null;
Private Mongomanager () {
} public
static DB Getdb (String dbname) {
try {
if (MONGO = = null) {
init (); c9/>}
catch (Exception e) {
logger.error (E.getmessage (), E);
}
Return Mongo.getdb (dbname);
}
private static void init () {
try {
mongoclient client = new Mongoclient ();
MONGO = Mongoclient.connect (127.0.0.1, 27017)). Getmongo ();
Mongooptions options = Mongo.getmongooptions ();
Options.sockettimeout = 20000;
Options.maxwaittime = 20000;
Options.connecttimeout = 20000;
} catch (Exception e) {
logger.error (E.getmessage (), E);
}
}
In the process of use if a large number of data insertion, each time data, tens data will often report the connection exception (connection timeout, can not find the corresponding db)
the check was found to be caused by a connection not being released ( db.serverstatus (). connections; )
Way to solve:
Discard using the Mongo function to use the mongoclient problem is solved
Attach the parameter introduction of the connection time:
If the connection is filled with queues, an "out of semaphores to get DB" error is thrown.
Maxwaittime: Maximum waiting for connection thread blocking time
ConnectTimeout: The millisecond of the connection timeout. 0 is the default and infinite
Sockettimeout:socket timeout. 0 is the default and infinite
Autoconnectretry: If this control is connected, the system will retry automatically