当前位置: 移动技术网 > IT编程>数据库>MongoDB > MongoDB使用过程中的报错处理(持续更新)

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

2019年05月23日  | 移动技术网IT编程  | 我要评论
1、连接池问题 解决com.mongodb.DBPortPool$SemaphoresOut: Out of semaphores to get db connection错误 Mongo reader = null;MongoOptions op = new MongoOptions();//处理 ...

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

 

 

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网