0

enter image description here I am new to java and Mongo I am trying to set a max pool size of 100

I am running Load test in localhost I am seeing that the pool is getting re-used without any issues but the max pool size is not following the pool size I have given

for 10k calls there there are 300 connections in the pool where I have given only 100

not sure how this is happening

can any one help

----------------------- Code Start ----------------------------------

public class MongoFactory {

    @Autowired
    Environment environment;
    private static MongoClient mongoClient;
    private static String connectionString;
    private static MongoDatabase database;
    private static String USER_NAME;
    private static String PASSWORD;
    private static boolean createCollection = true;
    @Autowired
    EnvironmentVarUtil envUtil;

    @Autowired
    MongoWrapper mongoWrapper;


    public void setConnectionString(String connectionString) {
        this.connectionString = connectionString;
    }

    public String getConnectionString() {
        return connectionString;
    }

    private static String mongoURIBuilder(String connectionString) {

            return "mongodb+srv://" + USER_NAME + ":" + PASSWORD + "@" + connectionString + "/dbname?retryWrites=true&w=majority&connectTimeoutMS=30000&socketTimeoutMS=30000&maxPoolSize=100";
    }


    private void initMongoDB() {
        if (USER_NAME == null) {
            USER_NAME = envUtil.getEnvVar("mongoUserName");
        }
        if (PASSWORD == null) {
            PASSWORD = envUtil.getEnvVar("mongoPassword");
        }

        String mongoURI = mongoURIBuilder(connectionString);
        MongoClientURI uri = new MongoClientURI(mongoURI);
        mongoClient = new MongoClient(uri);
        String dbName = "dbname";
        database = mongoClient.getDatabase(dbName);
    }

    
    


   
    public MongoObject load(MongoObject mongoObject) throws Exception
    {

        System.out.println("Start Time " +DateTime.now());
        if(mongoClient==null)
        {
            initMongoDB();
        }

        // Get from DB code

        System.out.println("End Time " +DateTime.now());
        return mongoObject;
    }

   


}
Ram Null
  • 40
  • 4
  • By default, the Java driver assigns a pool size of 100. But, you can set the pool size as per your need as shown in the following post [Managing Mongodb connections in Java as Object Oriented](https://stackoverflow.com/questions/60180119/managing-mongodb-connections-in-java-as-object-oriented/60337641#60337641) – prasad_ Feb 01 '21 at 09:38
  • I am setting the max pool size but its going beyond max pool size – Ram Null Feb 01 '21 at 11:32
  • and i tried with MongoClientSettings this also gave the same result – Ram Null Feb 01 '21 at 11:33
  • Please tell how do you know the pool size is going beyond the setting? Also, include the versions of MongoDB server (if its a replca-set, stand alone or sharded cluster), the Java driver version. It looks like you are working in the Spring Framework environment - please include the details about it as well (e.g., is it Spring Boot, etc.). – prasad_ Feb 01 '21 at 11:42
  • its a shraded cluster set and i am checking mongo atlas cloud and check no of active connections , I am working on mongo atlas cloud its the latest version – Ram Null Feb 01 '21 at 12:58
  • Please tell how do you know the pool size is going beyond the setting (logs, etc.) - post the actual info into the post? – prasad_ Feb 01 '21 at 13:58
  • I added the connection graph i am checking from mongo atlas to the question – Ram Null Feb 01 '21 at 14:04
  • Do you see any issues in the attached code which may cause the spillage – Ram Null Feb 01 '21 at 14:05
  • Here is some useful doc: [Atlas - Troubleshoot Connection Issues](https://docs.atlas.mongodb.com/troubleshoot-connection) – prasad_ Feb 01 '21 at 14:17
  • Are you closing the connections after you access the database and at the end of the process? – prasad_ Feb 01 '21 at 14:21

1 Answers1

0

Connection pools are maintained per known server. If your client knows of 3 servers (either 3 nodes in a replica set or 3 mongos routers for example), and you set max pool size to 100, you get up to 100 application connections to each of the servers for a total of 300 connections.

There are also 1 or 2 monitoring connections per server depending on driver and server version.

D. SM
  • 13,584
  • 3
  • 12
  • 21
  • Hi initially I also thought the same so I reduced the max connection pools size to 10 and tested it's still going to 250 - 300 in connection pool – Ram Null Feb 02 '21 at 12:21
  • Most likely you made a mistake in either configuration or your analysis. If you can reproduce this, report the bug to the driver. – D. SM Feb 02 '21 at 13:16
  • i Shared the code that i have written in the question can you look into it and let me know if I made any wrong implementation for creating – Ram Null Feb 02 '21 at 16:07
  • I don't see anything that either retrieves connection counts or shows what the counts actually are. – D. SM Feb 02 '21 at 18:25