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