SocketTimeout with opened connection in MongoDB

后端 未结 2 963
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-15 08:37

I\'ve a Java application that performs some aggregations on MongoDB, but sometimes it just hangs and throw a SocketTimeout exception. After the Exception the app will run ju

2条回答
  •  时光说笑
    2020-12-15 09:21

    After few tries I've found out that it was a problem with the Azure's Load Balancer.
    After 60s of inactivity it will disconnect any pending TCP connection.

    After further digging I've found this post of the MongoDB diagnostics FAQ, and I've set the tcp keepalive to 120s:

    sudo sysctl -w net.ipv4.tcp_keepalive_time=
    

    and I've also set the socketKeepAlive of the MongoClient to true:

    MongoClientOptions.Builder options = MongoClientOptions.builder();
    options.socketKeepAlive(true);
    mongoClient = new MongoClient(mongoAddress, options.build());
    

    After these fixes the issue seems gone!

提交回复
热议问题