MongoDB连接池耗尽

异常日志:

Out of semaphores to get db connection 查看源代码发现是连接池资源用尽: 

查代码看原因:

DBPortPool 写道
if ( ! _waitingSem.tryAcquire() ) throw new SemaphoresOut();

_waitingSem初始化代码

DBPortPool 写道
_waitingSem = new Semaphore( _options.connectionsPerHost * _options.threadsAllowedToBlockForConnectionMultiplier );
MongoOptions 写道
public MongoOptions(){

reset();

}

publicvoidreset(){

connectionsPerHost=Bytes.CONNECTIONS_PER_HOST;

threadsAllowedToBlockForConnectionMultiplier=5;

maxWaitTime=1000*60*2;

connectTimeout=0;

socketTimeout=0;

socketKeepAlive=false;

autoConnectRetry=false;

maxAutoConnectRetryTime=0;

slaveOk=false;

safe=false;

w=0;

wtimeout=0;

fsync=false;

j=false;

dbDecoderFactory=DefaultDBDecoder.FACTORY;

socketFactory=SocketFactory.getDefault();

}

其中:

static final int CONNECTIONS_PER_HOST = Integer.parseInt( System.getProperty( "MONGO.POOLSIZE" , "10" ) );

改变连接池大小:

1、可以通过系统属性改变连接池大小。

2、代码层面修改,new Mongo的时,先一个你需要的MongoOptions

相关推荐