在等待与可写服务器匹配的服务器时在 30000 毫秒后超时

2022-09-02 14:07:35

我在许多论坛上都看到了这个问题,但没有一个解决我的问题。我正在尝试将示例文档插入MongoDB DB。不幸的是,在插入过程中,我遇到了以下错误:colReceived.insert(doc)

SEVERE: Servlet.service() for servlet spring 抛出异常 com.mongodb.MongoTimeoutException:在等待与 WritableServerSelector 匹配的服务器时,在 30000 毫秒后超时。群集状态的客户端视图是 {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception open socket}, cause by {java.net.ConnectException: Connection refused: connect}}] at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:369)

这是我的连接ToDb方法

   MongoClient mongoClient = new MongoClient();

    // Now connect to your database
    DB db = mongoClient.getDB("test");
    System.out.println("connect to database successfully");

    DBCollection coll = db.createCollection("mycol", null);
    System.out.println("Collection created successfully");

    DBCollection colReceived = db.getCollection("mycol");
    System.out.println("Collection mycol selected successfully");

    BasicDBObject doc = new BasicDBObject("title", "MongoDB").append("description", "database").append("likes", 100)
            .append("url", "http://www.tutorialspoint.com/mongodb/").append("by", "tutorials point");

    colReceived.insert(doc);
    System.out.println("Document inserted successfully");

我的 pom.xml :

  <project xmlns="http://maven.apache.org/POM/4.0.0"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.objectdb.tutorial.spring</groupId>
<artifactId>Guestbook</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>Guestbook</name>
<url>http://maven.apache.org</url>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<repositories>
    <repository>
        <id>objectdb</id>
        <name>ObjectDB Repository</name>
        <url>http://m2.objectdb.com</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>com.objectdb</groupId>
        <artifactId>objectdb</artifactId>
        <version>2.6.2</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.1.0</version>
    </dependency>
    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>aopalliance</groupId>
        <artifactId>aopalliance</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.6.10</version>
    </dependency>

    <dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.2.2</version>
</dependency>


</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2</version>
                <executions>
                    <execution>
                        <id>enhance</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                        <configuration>
                            <mainClass>com.objectdb.Enhancer</mainClass>
                            <arguments>
                                <argument>guest.Guest</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.10</version>
                <configuration>
                    <scanIntervalSeconds>10</scanIntervalSeconds>
                    <stopKey>foo</stopKey>
                    <stopPort>9999</stopPort>
                </configuration>
                <executions>
                    <execution>
                        <id>start-jetty</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <scanIntervalSeconds>0</scanIntervalSeconds>
                            <daemon>true</daemon>
                        </configuration>
                    </execution>
                    <execution>
                        <id>stop-jetty</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
    <finalName>Guestbook</finalName>
</build>


答案 1

如果您在 mongod 日志中也有以下错误...(或类似)

错误:日志文件的可用空间不足 请在 /var/lib/mongodb/journal 中提供至少 3379MB,或使用 --smallfiles

您可以通过运行mongo作为mongod --smallfiles

否则,请尝试更新您的 mongo 驱动程序版本,以匹配您正在运行的服务器的版本。


答案 2

有同样的问题,在我的情况下,我使用的是Atlas MongoDB,我的ISP以随机的时间间隔为客户端轮换IP池。

所以我遇到了同样的错误,解决方案是将实际的IP地址添加到IP白名单中。


推荐