python mongodb连接池 您所在的位置:网站首页 mongodb连接池配置 python mongodb连接池

python mongodb连接池

2023-08-04 13:57| 来源: 网络整理| 查看: 265

一.mongoDB中的连接池

刚上手MongoDB,在做应用时,受以前使用关系型数据库的影响,会考虑数据库连接池的问题!

关系型数据库中,我们做连接池无非就是事先建立好N个连接(connection),并构建成一个连接池(connection pool),提供去连接和归还连接等操作。

而在MongoDB中,我们先来看看怎么进行操作,以insert为例:

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);

[伪代码]

如果套用以前的经验,可能会想偏,引用官方文档的一句话可能会豁然开朗。

Note: The Mongo object instance actually represents a pool of connections to the database; you will only 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 you decide to create many mongo intances, note that:

all resource usage limits (max connections, etc) apply per mongo instance

to dispose of an instance, make sure you call mongo.close() to clean up resources

mongo实例其实已经是一个现成的连接池了,而且线程安全。这个内置的连接池默认初始了10个连接,每一个操作(增删改查等)都会获取一个连接,执行操作后释放连接。

二.连接池的重要参数

内置连接池有多个重要参数,分别是:

connectionsPer



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有