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

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
 

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

 

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄