org.mongodb.driver.connection 太多:已打开文档数据库的连接

发布于 2025-01-12 20:50:06 字数 3008 浏览 1 评论 0原文

我们开发了一个 Arest 应用程序,它使用文档数据库来存储/检索用户数据。

MongoClientSettings settings =
                    MongoClientSettings.builder()
                            .applyToClusterSettings(builder ->
                                    builder.hosts(Arrays.asList(new ServerAddress(clusterEndpoint, 27017))))
                            .readPreference(ReadPreference.secondaryPreferred())
                            .retryWrites(false)
                            .applyToClusterSettings(builder ->
                                    builder.requiredClusterType(ClusterType.REPLICA_SET))
                            .applyToClusterSettings(builder ->
                                    builder.requiredReplicaSetName("rs0"))
                            .applyToClusterSettings(builder ->
                                    builder.mode(ClusterConnectionMode.MULTIPLE))
                            .applyToConnectionPoolSettings(builder ->
                                    builder.maxSize(Integer.parseInt(docDbAppConfig.get("maxSize"))))
                            .applyToConnectionPoolSettings(builder ->
                                    builder.minSize(Integer.parseInt(docDbAppConfig.get("minSize"))))
                            .applyToConnectionPoolSettings(builder ->
                                    builder.maxConnectionIdleTime(Integer.parseInt(docDbAppConfig.get("maxConnectionIdleTime")),
                                            TimeUnit.MILLISECONDS))
                            .credential(MongoCredential.createCredential(username, dbName, password.toCharArray()))
                            .applyToConnectionPoolSettings(builder ->
                                    builder.maxWaitTime(Integer.parseInt(docDbAppConfig.get("maxWaitTime")), TimeUnit.MINUTES))
                            .applyToClusterSettings(builder ->
                                    builder.serverSelectionTimeout(Integer.parseInt(docDbAppConfig.get("serverSelectionTimeout")),
                                            TimeUnit.SECONDS))
                            .applyToSocketSettings(builder ->
                                    builder.connectTimeout(Integer.parseInt(docDbAppConfig.get("connectTimeout")), TimeUnit.SECONDS))
                            .applyToSocketSettings(builder ->
                                    builder.readTimeout(Integer.parseInt(docDbAppConfig.get("readTimeout")), TimeUnit.SECONDS))
                            .build();

在我们的应用程序中,我们使用了 Dao 模型并确保正确创建连接并正确关闭连接。连接打开和关闭均通过应用程序日志进行验证。

我们使用 db.r5.4xlarge 服务器作为文档数据库,该服务器根据用户负载限制为 14K 连接/游标。当人们继续读取/写入数据时,应用程序工作正常,但是我们看到一种行为,即连接变高并在一段时间内保持相同状态,除非重新启动应用程序。在此期间观察到以下日志,重新启动应用程序后,连接计数恢复到正常水平。

(MaintenanceTimer-5883-thread-1) org.mongodb.driver.connection: Opened connection [connectionId{localValue:61248}] 

为了调试它,我们验证了所有工作流程及其连接打开和关闭的日志,但没有发现异常。我们在创建连接时也添加了调试语句,但这也没有帮助。

如果有人可以分享他们的专业知识并协助解决这个问题,我们将不胜感激。

We have developed an Arest application that uses document DB to store/retrieve user data.

MongoClientSettings settings =
                    MongoClientSettings.builder()
                            .applyToClusterSettings(builder ->
                                    builder.hosts(Arrays.asList(new ServerAddress(clusterEndpoint, 27017))))
                            .readPreference(ReadPreference.secondaryPreferred())
                            .retryWrites(false)
                            .applyToClusterSettings(builder ->
                                    builder.requiredClusterType(ClusterType.REPLICA_SET))
                            .applyToClusterSettings(builder ->
                                    builder.requiredReplicaSetName("rs0"))
                            .applyToClusterSettings(builder ->
                                    builder.mode(ClusterConnectionMode.MULTIPLE))
                            .applyToConnectionPoolSettings(builder ->
                                    builder.maxSize(Integer.parseInt(docDbAppConfig.get("maxSize"))))
                            .applyToConnectionPoolSettings(builder ->
                                    builder.minSize(Integer.parseInt(docDbAppConfig.get("minSize"))))
                            .applyToConnectionPoolSettings(builder ->
                                    builder.maxConnectionIdleTime(Integer.parseInt(docDbAppConfig.get("maxConnectionIdleTime")),
                                            TimeUnit.MILLISECONDS))
                            .credential(MongoCredential.createCredential(username, dbName, password.toCharArray()))
                            .applyToConnectionPoolSettings(builder ->
                                    builder.maxWaitTime(Integer.parseInt(docDbAppConfig.get("maxWaitTime")), TimeUnit.MINUTES))
                            .applyToClusterSettings(builder ->
                                    builder.serverSelectionTimeout(Integer.parseInt(docDbAppConfig.get("serverSelectionTimeout")),
                                            TimeUnit.SECONDS))
                            .applyToSocketSettings(builder ->
                                    builder.connectTimeout(Integer.parseInt(docDbAppConfig.get("connectTimeout")), TimeUnit.SECONDS))
                            .applyToSocketSettings(builder ->
                                    builder.readTimeout(Integer.parseInt(docDbAppConfig.get("readTimeout")), TimeUnit.SECONDS))
                            .build();

In our application we have used Dao model and ensure connections are created correctly and closed properly too. Both connection open and close is verified from application logs.

We are using the db.r5.4xlarge server for document DB which has a limit of 14K connections/cursors based on user load. The application works fine when people continue to read/write data, however we are seeing a behaviour where connection goes high and remains in same state for a while unless application is restarted. Below logs are observed during this and after restarting application, connection counts goes back to normal level.

(MaintenanceTimer-5883-thread-1) org.mongodb.driver.connection: Opened connection [connectionId{localValue:61248}] 

In order to debug it, we verified all the workflows and their logs for connection open and close, but no anomaly was found. We added debug statements as well when connections are created but that did not help too.

Appreciate if anyone can share their expertise and assist in overcoming this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谢绝鈎搭 2025-01-19 20:50:06

看起来旧连接没有关闭,但新连接正在打开。请检查不同的超时值。

Looks like old connections are not closing but new connections are getting opened. please check the different timeout values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文