一、鏈接池問題java
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
/*
* 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();
}
}
}socket