2

My program requires a large number of connections to be open (Mongo). I get the error :

Too many connections open, can't open anymore

after 819 connections. I already know we can increase this limit. But that's not what I have in mind. I'm thinking of closing the MongoClient object, and then creating a new one again after 800 connections.

My thinking is that with a new mongoClient object all the connections will be closed and when I start/create it again, the connections will be opened again until 800. Thus not giving the error. (Let me know if this approach is totally wrong/ won't give the required results.)

For this I need to know the number of connections opened ATM. Is there any way to get this information using java?

ravisvi
  • 149
  • 2
  • 9
  • 1
    Why do you have so many open in the first place? MongoClient already has a connection pool internally. You should just create one and reuse that. Are you creating a lot of MongoClients? – evanchooly Jan 21 '14 at 15:19
  • The program I'm using is for research purposes, trying to generate a lot of data and analyze it. Hence so many connections. (I'm using multithreading) I have a few mongo clients. – ravisvi Jan 21 '14 at 16:52
  • 2
    Possible duplicate of [Check the current number of connections to MongoDb](https://stackoverflow.com/questions/8975531/check-the-current-number-of-connections-to-mongodb) – Inanc Gumus Jul 17 '17 at 13:13
  • With this you could create a singleton connector https://stackoverflow.com/a/26085824/2093371 – hestellezg Apr 20 '18 at 17:07

2 Answers2

3

You can get connection information by using the db.serverStatus() command. It has a connections subdocument which contains the total/available connections information.

For more information :

Parvin Gasimzade
  • 25,180
  • 8
  • 56
  • 83
1

Check the number of MongoDB connections using MongoDB Scala driver:

  1. Create a MongoDB client:
import org.mongodb.scala._
import scala.collection.JavaConverters._
import scala.concurrent.Await
import scala.concurrent.duration._
import scala.util.{Failure, Success, Try}

// To directly connect to the default server localhost on port 27017
val mongodbClient: MongoClient = MongoClient()

// Use a Connection String
val mongodbClient: MongoClient = MongoClient("mongodb://localhost")

// or provide custom MongoClientSettings
val settings: MongoClientSettings = MongoClientSettings.builder()
    .applyToClusterSettings(b => b.hosts(List(new ServerAddress("localhost")).asJava).
    .build()
val mongodbClient: MongoClient = MongoClient(settings)
  1. Call getNoOfMongodbConnection by passing mongodbClient:
val result = getNoOfMongodbConnection(mongodbClient)
  1. Method to get the number of connections(current, available and total)
def getNoOfMongodbConnection(mongodbClient: MongoClient) = {

    val adminDatabase = mongodbClient.getDatabase("admin")

    val serverStatus = adminDatabase.runCommand(Document("serverStatus" -> 1)).toFuture()

    Try {
      Await.result(serverStatus, 10 seconds)

    } match {
      case Success(x) => {

        val connection = x.get("connections")
        logger.info("Number of mongodb connection:--> " + connection)
        connection
      }
      case Failure(ex) => {
        logger.error("Got error while getting the number of Mongodb connection:---> " + ex.printStackTrace())
        None
      }
    }
  }
Keshav Lodhi
  • 2,641
  • 2
  • 17
  • 23