morphia client always makes more than 1 connection

25 Views Asked by At

Trying out morphia client to connect to mongodb , I do find that morphia always makes more than 1 connection even when we have strictly set both min and max size to 1

whats the right way to use connection pool and restrict to 1 connection only ?

code here

public static void main(String[] args) {
        ConnectionPoolSettings connectionPoolSettings = ConnectionPoolSettings.builder().minSize(1).maxSize(1).build();

        MongoClient client = MongoClients.create( MongoClientSettings
                        .builder()
                        .applyConnectionString(new ConnectionString("mongodb://localhost:27017/test"))
                        .applyToConnectionPoolSettings(builder -> builder.applySettings(connectionPoolSettings))
                        .build());
        MongoDatabase database = client.getDatabase("admin");
        Document serverStatus = database.runCommand(new Document("serverStatus", 1));

        new Thread(() -> {
            while(true) {
                Map connections = (Map) serverStatus.get("connections");
                System.out.println("Number of connections " + connections.get("current"));
                try {
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
        try {

            while(true)
                TimeUnit.SECONDS.sleep(60);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
raaghu@FVFH8ET1Q05D ~ % netstat -anv | awk '$5 ~ "27017" {print}'| grep ESTABLISHED
tcp4       0      0  127.0.0.1.57851        127.0.0.1.27017        ESTABLISHED  337773  146988  27252      0 00002 00000008 000000000008ef3a 00000080 00000800      1      0 000001
tcp4       0      0  127.0.0.1.57850        127.0.0.1.27017        ESTABLISHED  407373  146988  27252      0 00002 00000008 000000000008ef21 00000080 00000800      1      0 000001
tcp4       0      0  127.0.0.1.57849        127.0.0.1.27017        ESTABLISHED  407373  146988  27252      0 00002 00000008 000000000008ef20 00000080 00000800      1      0 000001

0

There are 0 best solutions below