当前位置: 首页 > 知识库问答 >
问题:

为什么当我运行我的迷你spring启动项目时,与mongodb的连接会关闭?

松亦
2023-03-14

我是一个初学者(顺便说一下:)

错误如下:

org.mongodb.driver.connection: Closed connection [connectionId{localValue:3, serverValue:4}] to localhost:27017 because the pool has been closed

我的主要课程是:

@SpringBootApplication
public class MangoApplication implements CommandLineRunner {

    @Autowired
    private CustomerRepository repository;
    public static void main(String[] args) {
        SpringApplication.run(MangoApplication.class, args);
    }
    @Override
    public void run(String... args) throws Exception {

        repository.deleteAll();

        // save a couple of customers
        repository.save(new Customer("Alice", "Smith"));
        repository.save(new Customer("Bob", "Smith"));

        // fetch all customers
        System.out.println("Customers found with findAll():");
        System.out.println("-------------------------------");
        for (Customer customer : repository.findAll()) {
            System.out.println(customer);
        }
        System.out.println();

        // fetch an individual customer
        System.out.println("Customer found with findByFirstName('Alice'):");
        System.out.println("--------------------------------");
        System.out.println(repository.findByFirstName("Alice"));

        System.out.println("Customers found with findByLastName('Smith'):");
        System.out.println("--------------------------------");
        for (Customer customer : repository.findByLastName("Smith")) {
            System.out.println(customer);
        }

    }

}

我的CustomerRepository接口

public interface CustomerRepository extends MongoRepository<Customer, String> {
    public Customer findByFirstName(String firstName);
    public List<Customer> findByLastName(String lastName);
}

共有1个答案

韩鸿
2023-03-14

添加mongoClient bean。

@Bean
public MongoClient mongoClient(){
    return new MongoClient( "localhost" , 27017 );
}
 类似资料:
  • 我已经从github下载了代码,并通过npm Install安装了所有需要的节点模块。npm i(&N)。但我仍然面临着这种错误

  • 无法在Android上启动项目。 错误:找不到与Android连接的设备,也无法自动启动模拟器。 请连接设备或创建模拟器(https://docs.expo.io/workflow/android-studio-emulator). 然后按照这里的说明启用USB调试: https://developer.android.com/studio/run/device.html#developer-de

  • 认证之后,如果我调用任何方法,比如< code>os.compute()。口味()。list()或< code>os.images()。list(),我得到< code >连接超时。为什么会这样? 我在GoogleCloudsPlataform VM上设置了一个带有RDO包堆栈的OpenStack。我正在对域和项目进行身份验证。我尝试了没有项目的身份验证,方法调用没有超时,但是响应是错误的,例如,

  • 文件中说: 设置代理的HTTP协议版本。默认情况下,使用1.0版。建议将1.1版用于keepalive连接和NTLM身份验证。 在我的nginx配置中我有 直接http://127.0.0.1:1980,我可以看到我的应用程序在一个连接上收到许多请求(当我刷新时)。这是我发送的响应 HTTP/1.1 200 OK\n内容类型:text/html\n内容长度:14\n连接:保持活动\n\n Hell

  • 我在三个EC2实例中有一个MongoDB客户机,并创建了一个副本集。上一次我遇到了一个问题,空间限制导致mongod进程停止,从而使应用程序停止,现在在几天前的一个实例中,我的一些表从数据库中消失了,所以我将日志记录和所有设置到数据库中,以便捕捉再次发生类似情况的情况。在今天早上的一个新事件中,我无法登录到我的系统,这时我发现整个数据库都是空的。我检查了其他类似这样的问题,建议设置TTL.,但我根

  • 当我单击“确定”时,我收到一条错误消息:“另一个应用程序正在侦听端口8888。请检查您的端口配置”。 我还成功地用8888和Java任务控制(JMC)评测了这台机器。然而,JMC要求我提供用户名和密码,然后才允许连接。