Using a connection pool in the MongoDB 2.6 C + + driver

Source: Internet
Author: User
Tags connection pooling creative commons attribution

MongoDB2.6 's Cxx driver (Mongo-cxx-driver-26compat), built-in with a database connection pool, makes it easy to manage database connections, but official documentation is obscure and novice beginners often don't know how to use connection pooling to get a database connection. This article briefly describes how to use a basic connection pool.

The first thing to make clear is that although there is a connection pool class, there is no need to display the class to be called, using the following method:

Mongo::scopeddbconnection scopedconn = mongo::scopeddbconnection (dbhost); MONGO::D bclientbase* conn = ScopedConn.get ( );

The code is interpreted as follows, the first line uses the Mongo::scopeddbconnection object, which is responsible for getting a specified database connection from the built-in database connection pool, Dbhost represents the database address, and the native test is "127.0.0.1:27017". Then from the linked object can get the specific database connection operation object Pointer Dbclientbase, for Insert,remove,update,query,findone and other operations.

Note When you are finished using the connection, you need to call the following code to release the link, otherwise the connection will never be released causing the problem of creating a new connection:

Scopedconn.done ();

In general, the use of MongoDB connection pool is very simple, if the official has a practical example to tell users how to use will be better understand


This work is licensed under the Creative Commons Attribution-NonCommercial use-Share 4.0 International license agreement in the same way.
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.