Slow Connection in Mongo SSL/TLS using Java Driver

Hi, apparently we notice there is a significant slowness in connecting to MongoDB SSL/TLS. We have tested in both SSL in version 4.0.16 and TLS in version 4.2.7 using mongodb-driver-sync version 4.0.4 (or version 3.9.x above). It takes around 9 seconds.

The issue does not seem to appear when we are using mongodb-driver version 3.8.2. It only takes around 400 milliseconds. We are using docker-based MongoDB.

This is our sample testing code. Your help would be appreciated. Thanks!

public class TestWithoutSpring {

    private SSLContext sslContext;

    public void before() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        sslContext = SSLContextBuilder.create()
                .loadTrustMaterial(null, (chain, authType) -> true)

    public void testMongoClientNew() {
        ConnectionString connectionString = new ConnectionString("mongodb://");
        MongoClientSettings settings = MongoClientSettings.builder().applyConnectionString(connectionString)
                .applyToSslSettings(builder -> builder.context(sslContext).invalidHostNameAllowed(true))
        com.mongodb.client.MongoClient client = MongoClients.create(settings);
        String database = connectionString.getDatabase();
        long start = System.currentTimeMillis();
        client.getDatabase(database).runCommand(new Document("ping", 1));
        long end = System.currentTimeMillis();
        System.err.println(end - start);