JAVA Connection MongoDB Practice (1) __java

Source: Internet
Author: User
Tags getmessage mongoclient mongodb


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



 


   


  





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.