Spring boot 2.0.5.RELEASE and mongo 4.0 connection issues

16,509

Solution 1

Look at the method not found log presented by spring boot.

com.mongodb.connection.DefaultClusterFactory.createCluster( Lcom/mongodb/connection/ClusterSettings; Lcom/mongodb/connection/ServerSettings; Lcom/mongodb/connection/ConnectionPoolSettings; Lcom/mongodb/connection/StreamFactory; Lcom/mongodb/connection/StreamFactory; Ljava/util/List;Lcom/mongodb/event/CommandListener; Ljava/lang/String;Lcom/mongodb/client/MongoDriverInformation; Ljava/util/List;)Lcom/mongodb/connection/Cluster;

Note the 7th argument Lcom/mongodb/client/MongoDriverInformation. The MongoDriverInformation class has been moved from com/mongodb/client to com/mongodb in 3.7 beyond versions.

So it appears that you have both 3.6.4 and 3.8.0 jars on your classpath.

So try to clean your classpath and <mongodb.version>3.8.0</mongodb.version> is the correct way of overriding mongodb dependencies until spring boot release.

Btw there is pending 2.1.0 boot release which will update the mongodb dependency to 3.8.2 Right now it is a release candidate stage.

Solution 2

I also met this problem. when I first added pom:

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver</artifactId>
        <version>3.4.3</version>
    </dependency>

After compiling the project, it occured:

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call the method com.mongodb.connection.DefaultClusterFactory.createCluster(Lcom/mongodb/connection/ClusterSettings;Lcom/mongodb/connection/ServerSettings;Lcom/mongodb/connection/ConnectionPoolSettings;Lcom/mongodb/connection/StreamFactory;Lcom/mongodb/connection/StreamFactory;Ljava/util/List;Lcom/mongodb/event/CommandListener;Ljava/lang/String;Lcom/mongodb/client/MongoDriverInformation;Ljava/util/List;)Lcom/mongodb/connection/Cluster; but it does not exist. Its class, com.mongodb.connection.DefaultClusterFactory, is available from the following locations:

jar:file:/C:/Users/winUser/.m2/repository/org/mongodb/mongodb-driver-core/3.8.0/mongodb-driver-core-3.8.0.jar!/com/mongodb/connection/DefaultClusterFactory.class

It was loaded from the following location:

file:/C:/Users/winUser/.m2/repository/org/mongodb/mongodb-driver-core/3.8.0/mongodb-driver-core-3.8.0.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of com.mongodb.connection.DefaultClusterFactory

Then I corrected the pom, I solved this mistake, but I saw another mistake: This is the new pom, and i think its just owing to mongo_version.

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver</artifactId>
        <version>3.8.0</version>
    </dependency>

This is the new warning:

com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:67) ~[mongodb-driver-core-3.8.2.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.8.2.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-3.8.2.jar:na]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_191]
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_191]
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_191]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_191]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_191]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_191]
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_191]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_191]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_191]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongodb-driver-core-3.8.2.jar:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.8.2.jar:na]
... 3 common frames omitted

So this problem is so easy and network has many methods to solve it, you just add one annotation to let spring boot ignore default mongo_properties:

@SpringBootApplication(exclude = MongoAutoConfiguration.class)

Then done!

Solution 3

I think this article on Overriding dependency versions with Spring Boot will help you. For reactor replace with mongodb. The MongoDB drivers themselves are pretty-much backwards compatible so you shouldn't have any big problems forcing a newer version.

Share:
16,509

Related videos on Youtube

JavaSheriff
Author by

JavaSheriff

Updated on June 04, 2022

Comments

  • JavaSheriff
    JavaSheriff almost 2 years

    I am following the Accessing Data with MongoDB tutorial on Spring web site
    I Installed Mongo DB server version 4 as a service
    Its authentication is working fine when I connect to it using a a client.

    I am facing the issue below:

    MongoCommandException: Command failed with error 18: 'Authentication failed'
    

    I see that the code is using mongodb-driver-core-3.6.4.jar that is not compatible with server version 4

    How to go about upgrading the driver only without messing the project?
    Why are they using spring-boot-starter-data-mongodb and not mongodb-driver-sync ?

    com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='petUser', source='pets', password=<hidden>, mechanismProperties={}}
        at com.mongodb.connection.SaslAuthenticator.wrapException(SaslAuthenticator.java:162) ~[mongodb-driver-core-3.6.4.jar:na]
        at com.mongodb.connection.SaslAuthenticator.access$200(SaslAuthenticator.java:39) ~[mongodb-driver-core-3.6.4.jar:na]
        at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:68) ~[mongodb-driver-core-3.6.4.jar:na]
        at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:46) ~[mongodb-driver-core-3.6.4.jar:na]
        at com.mongodb.connection.SaslAuthenticator.doAsSubject(SaslAuthenticator.java:168) ~[mongodb-driver-core-3.6.4.jar:na]
        at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:46) ~[mongodb-driver-core-3.6.4.jar:na]
        at com.mongodb.connection.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32) ~[mongodb-driver-core-3.6.4.jar:na]
        at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:122) ~[mongodb-driver-core-3.6.4.jar:na]
        at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:52) ~[mongodb-driver-core-3.6.4.jar:na]
        at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:127) ~[mongodb-driver-core-3.6.4.jar:na]
        at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114) ~[mongodb-driver-core-3.6.4.jar:na]
        at java.lang.Thread.run(Unknown Source) [na:1.8.0_181]
    Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server localhost:27014. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }
        at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:164) ~[mongodb-driver-core-3.6.4.jar:na]
        at com.mongodb.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:295) ~[mongodb-driver-core-3.6.4.jar:na]
        at com.mongodb.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255) ~[mongodb-driver-core-3.6.4.jar:na]
        at com.mongodb.connection.CommandHelper.sendAndReceive(CommandHelper.java:84) ~[mongodb-driver-core-3.6.4.jar:na]
        at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:34) ~[mongodb-driver-core-3.6.4.jar:na]
        at com.mongodb.connection.SaslAuthenticator.sendSaslStart(SaslAuthenticator.java:119) ~[mongodb-driver-core-3.6.4.jar:na]
        at com.mongodb.connection.SaslAuthenticator.access$000(SaslAuthenticator.java:39) ~[mongodb-driver-core-3.6.4.jar:na]
        at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:52) ~[mongodb-driver-core-3.6.4.jar:na]
        ... 9 common frames omitted
    

    Here is the project POM:

    <?xml version="1.0" encoding="UTF-8"?>
    <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/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.springframework</groupId>
        <artifactId>gs-accessing-data-mongodb</artifactId>
        <version>0.1.0</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.5.RELEASE</version>
        </parent>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-mongodb</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
        <repositories>
            <repository>
                <id>spring-releases</id>
                <name>Spring Releases</name>
                <url>https://repo.spring.io/libs-release</url>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>spring-releases</id>
                <name>Spring Releases</name>
                <url>https://repo.spring.io/libs-release</url>
            </pluginRepository>
        </pluginRepositories>
    </project>
    

    Update
    maven dependency-plugin show dependency on mongodb:mongodb-driver-core:jar:3.8.0
    So Im not sure where is this mongodb-driver-core-3.6.4.jar coming from?

    [INFO] --- maven-dependency-plugin:3.0.2:tree (default-cli) @ demo ---
    [INFO] com.replaceme:demo:jar:0.0.1-SNAPSHOT
    [INFO] +- org.springframework.boot:spring-boot-starter-data-mongodb:jar:2.0.5.RELEASE:compile
    [INFO] |  +- org.springframework.boot:spring-boot-starter:jar:2.0.5.RELEASE:compile
    [INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.5.RELEASE:compile
    [INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.2.3:compile
    [INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.2.3:compile
    [INFO] |  |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile
    [INFO] |  |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.10.0:compile
    [INFO] |  |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
    [INFO] |  |  +- javax.annotation:javax.annotation-api:jar:1.3.2:compile
    [INFO] |  |  \- org.yaml:snakeyaml:jar:1.19:runtime
    [INFO] |  +- org.mongodb:mongodb-driver:jar:3.8.0:compile
    [INFO] |  |  +- org.mongodb:bson:jar:3.8.0:compile
    [INFO] |  |  \- org.mongodb:mongodb-driver-core:jar:3.8.0:compile
    [INFO] |  \- org.springframework.data:spring-data-mongodb:jar:2.0.10.RELEASE:compile
    [INFO] |     +- org.springframework:spring-context:jar:5.0.9.RELEASE:compile
    [INFO] |     +- org.springframework:spring-beans:jar:5.0.9.RELEASE:compile
    [INFO] |     +- org.springframework:spring-expression:jar:5.0.9.RELEASE:compile
    [INFO] |     +- org.springframework.data:spring-data-commons:jar:2.0.10.RELEASE:compile
    

    Update 2
    I removed all mongodb jars from my M2 folder
    and added mongodb properties to the pom section:

        <mongodb.version>3.8.0</mongodb.version>
    

    Now I see a new error:

    ***************************
    APPLICATION FAILED TO START
    ***************************
    
    Description:
    
    An attempt was made to call the method com.mongodb.connection.DefaultClusterFactory.createCluster(Lcom/mongodb/connection/ClusterSettings;Lcom/mongodb/connection/ServerSettings;Lcom/mongodb/connection/ConnectionPoolSettings;Lcom/mongodb/connection/StreamFactory;Lcom/mongodb/connection/StreamFactory;Ljava/util/List;Lcom/mongodb/event/CommandListener;Ljava/lang/String;Lcom/mongodb/client/MongoDriverInformation;Ljava/util/List;)Lcom/mongodb/connection/Cluster; but it does not exist. Its class, com.mongodb.connection.DefaultClusterFactory, is available from the following locations:
    
        jar:file:/C:/Users/winUser/.m2/repository/org/mongodb/mongodb-driver-core/3.8.0/mongodb-driver-core-3.8.0.jar!/com/mongodb/connection/DefaultClusterFactory.class
    
    It was loaded from the following location:
    
        file:/C:/Users/winUser/.m2/repository/org/mongodb/mongodb-driver-core/3.8.0/mongodb-driver-core-3.8.0.jar
    
    
    Action:
    
    Correct the classpath of your application so that it contains a single, compatible version of com.mongodb.connection.DefaultClusterFactory
    

    Update 3

    It is running fine outside eclipse,
    In command line I did:

    mvn package  
    cd target
    java -jar .\demo-0.0.1-SNAPSHOT.jar
    

    And its running fine, not sure what is the issue within Eclipse.

    • s7vr
      s7vr over 5 years
      It is not clear what your question. Is the question regarding the mongo java driver mismatch ? Or is it about the exception ? There is not enough information in the post to analyse what could be causing java driver mismatch. Regarding the exception it looks like your security is not set up properly. You have to add the code where you are creating connection and passing user credentials.
    • rootExplorr
      rootExplorr over 5 years
      Why do you think it is a jar issue?
    • rootExplorr
      rootExplorr over 5 years
      Did you create a user inside MongoDB and try to use that to access the DB ?
    • s7vr
      s7vr over 5 years
      If the version mismatch issue is resolved you can add the code/configuration where you create the connection or mongo template and we can verify if you have the authorization set up correctly. . You need to pass the authentication database name if you are using db name other than admin. Something like spring.data.mongodb.authentication-database=yourauthenticati‌​ondb
    • Sumanth Varada
      Sumanth Varada over 4 years
      facing the same issue even with mongo db 3.8.2 version. I'm using spring data to connect with aws documentDB.
  • s7vr
    s7vr over 5 years
    I'm not entirely sure where is 3.6.4 coming from because it should be 3.8.0 based on your property.. Check the target folder to make sure it is not packaged with the artifact. It shouldn't be inside the artifact.
  • s7vr
    s7vr over 5 years
    Btw how are you building/ packaging and running your code ? Do you use spring boot maven plugin ? or Is it all through maven ?
  • s7vr
    s7vr over 5 years
    Okay. May be eclipse is not picking up changes from your pom.Can you check the deployment assembly in the eclipse ? Have you tried project -> clean & right click pom with maven - update project eclipse ?