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

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

 

 

转载于:https://www.cnblogs.com/liyingxiao/p/10905793.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值