MongoDB:IllegalStateException:Spring Boot 应用程序中的池已关闭错误消息 [英] MongoDB: IllegalStateException: The pool is closed error message in Spring Boot application

查看:27
本文介绍了MongoDB:IllegalStateException:Spring Boot 应用程序中的池已关闭错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此信息消息:

19-October-24 08:05:53:481 INFO Thread-4 o.m.d.connection:71 - 关闭连接 [connectionId{localValue:35, serverValue:38}] 到mongodb:27017 因为池已关闭.

19-October-24 08:05:53:481 INFO Thread-4 o.m.d.connection:71 - Closed connection [connectionId{localValue:35, serverValue:38}] to mongodb:27017 because the pool has been closed.

还有以下错误:

java.lang.IllegalStateException: 池关闭于com.mongodb.internal.connection.ConcurrentPool.get(ConcurrentPool.java:137)在

java.lang.IllegalStateException: The pool is closed at com.mongodb.internal.connection.ConcurrentPool.get(ConcurrentPool.java:137) at

java.lang.IllegalStateException:状态应该是:open atcom.mongodb.assertions.Assertions.isTrue(Assertions.java:70)

java.lang.IllegalStateException: state should be: open at com.mongodb.assertions.Assertions.isTrue(Assertions.java:70)

这就是我创建 MongoClient 的方式,简单明了:

This is how I'm creating the MongoClient, plain and simple:

@Bean
@Override
public MongoClient mongoClient() {
    return new MongoClient(host);
}

这个 SO answer 建议设置 socketKeepAlive(true) 但据我所知此方法已弃用,因为它默认为 true.

This SO answer suggests to set socketKeepAlive(true) but as far as I understand this method is deprecated as it's true by default.

  • 我正在使用 MongoTemplate 和 MongoRepository.
  • 应用程序(如上所述)是多线程的.

我想了解错误的含义是什么?(即为什么游泳池会被关闭?).

I'd like to understand what's the meaning of the error ? (i.e. Why would the pool ever be closed?).

我是否需要设置/调整一些 Spring-Boot 参数?我需要以不同的方式构建 MongoClient 吗?

Do I need to set/adjust some Spring-Boot parameters maybe? Do I need to build MongoClient differently?

推荐答案

此错误意味着您的 MongoDB 连接因某种原因关闭,而您正尝试使用此连接池.

This error means your MongoDB connections are closed for some reason and you are trying to use this connection pool.

如果您使用的是 springs 连接池,则可以在没有 spring 的情况下创建连接池,并且可以在关闭时管理连接.(比如错误时重新连接)

If you are using springs connection pool you can create your connection pool without spring and you can manage the connection when is closed. ( Like reconnection on errors )

如果您正在进行多线程操作,请更改您的 MongoClient beans Scope 并创建它 基于线程.Mongoclient 在后台创建一个连接池,并将已经连接的连接池提供给新创建的客户端,因此基于线程的客户端不会在每次自动装配操作时创建连接.

If you are doing multithreaded operations change your MongoClient beans Scope and create it thread based. Mongoclient creates a connection pool in the background and gives already pooled connections to newly created clients so thread-based clients will not create connection on every autowire operation.

如果您想使用 socketKeepAlive 功能,您需要提供如下选项:

If you want to use the socketKeepAlive feature you need to give options like this:

MongoClientOptions options = MongoClientOptions.builder()
                .socketKeepAlive(false)
                .build();

MongoClient mongoClient = new MongoClient( "yourhost:mongoport" ,  options);

这篇关于MongoDB:IllegalStateException:Spring Boot 应用程序中的池已关闭错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆