MongoDB使用过程中的报错处理(持续更新)

栏目: 数据库 · 发布时间: 6年前

内容简介:1、连接池问题

1、连接池问题

com.mongodb.DBPortPool$SemaphoresOut  Concurrent requests for database connection have exceeded limit 50

#解决办法
MongoDB默认的连接数一般不会低于50,先通过mongostat查看当前连接数使用情况,再通过db.serverStatus().connections查看数据库的当前和最大连接数,排除服务端问题后,查看应用程序代码端是不是配置的连接池部分少了,这里以 java 语言为例。

解决com.mongodb.DBPortPool$SemaphoresOut: Out of semaphores to get db connection错误 Mongo reader = null;MongoOptions op = new 
MongoOptions();//处理 Out of semaphores to get db 
connectionop.setConnectionsPerHost(200);
op.setThreadsAllowedToBlockForConnectionMultiplier(50);
reader = new Mongo(DBConfig.getValue("mongoReadIp")+":27017",op); 
reader.slaveOk();

/*
 * mongodb数据库链接池
 */
public class MongoDBDaoImpl implements MongoDBDao
{
    private MongoClient mongoClient = null;
    private static final MongoDBDaoImpl mongoDBDaoImpl = new MongoDBDaoImpl();// 饿汉式单例模式

 private MongoDBDaoImpl()
    {
        if (mongoClient == null)
        {
            MongoClientOptions.Builder buide = new MongoClientOptions.Builder();
            buide.connectionsPerHost(100);// 与目标数据库可以建立的最大链接数
            buide.connectTimeout(1000 * 60 * 20);// 与数据库建立链接的超时时间
            buide.maxWaitTime(100 * 60 * 5);// 一个线程成功获取到一个可用数据库之前的最大等待时间
            buide.threadsAllowedToBlockForConnectionMultiplier(100);
            buide.maxConnectionIdleTime(0);
            buide.maxConnectionLifeTime(0);
            buide.socketTimeout(0);
            buide.socketKeepAlive(true);
            MongoClientOptions myOptions = buide.build();
            try
            {
                mongoClient = new MongoClient(new ServerAddress("127.0.0.1", 27017), myOptions);
            } catch (UnknownHostException e)
            {
                e.printStackTrace();
            }
        }
    }
 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Joy of X

The Joy of X

Niall Mansfield / UIT Cambridge Ltd. / 2010-7-1 / USD 14.95

Aimed at those new to the system seeking an overall understanding first, and written in a clear, uncomplicated style, this reprint of the much-cited 1993 classic describes the standard windowing syste......一起来看看 《The Joy of X》 这本书的介绍吧!

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具