timed out after 30000 ms while waiting for a server that matches WritableServerSelector

27,608

Solution 1

If you also have the following error in your mongod logs... (or similar)

ERROR: Insufficient free space for journal files Please make at least 3379MB available in /var/lib/mongodb/journal or use --smallfiles

You can fix this error by running mongo as mongod --smallfiles

Otherwise, try updating your mongo-driver version to match that of the server you are running.

Solution 2

Had the same problem, in my case I was using Atlas MongoDB and my ISP was rotating IP pool for clients at random time intervals.

So I've had the same error and the solution was adding actual IP address to the IP whitelist.

Solution 3

Had the same exception in my case when I was trying to perform update on Mongo collection.

The issue on my case was that I was using replica sets and the connection string was pointing to Secondary cluster. Due to this, I was able to read from the cluster but was unable to perform write(insert, update) operations. So, changing the connection string to point Primary Cluster fixed this issue for me.

Share:
27,608
Tonyukuk
Author by

Tonyukuk

Updated on July 09, 2022

Comments

  • Tonyukuk
    Tonyukuk almost 2 years

    I have seen this problem at many forums but none of them resolved my issue. I am trying to insert a sample document to MongoDB DB. Unfortunately, during the insert process colReceived.insert(doc) I got following error:

    SEVERE: Servlet.service() for servlet spring threw exception com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}] at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:369)

    Here is my connectToDb method

       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");
    

    My 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>