0

This is my first time trying anything with MongoDB, so I would love any tips if you guys have any, but in particular, Im having trouble establishing a connection... This error happens when the jar loads.

This is my stacktrace:

[00:12:43 INFO]: Exception in monitor thread while connecting to server 127.0.0.1:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[GangWars-1.0-SNAPSHOT-shaded.jar:?]
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[GangWars-1.0-SNAPSHOT-shaded.jar:?]
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) [GangWars-1.0-SNAPSHOT-shaded.jar:?]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[?:1.8.0_91]
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) ~[?:1.8.0_91]
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[?:1.8.0_91]
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[?:1.8.0_91]
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[?:1.8.0_91]
    at java.net.PlainSocketImpl.connect(Unknown Source) ~[?:1.8.0_91]
    at java.net.SocksSocketImpl.connect(Unknown Source) ~[?:1.8.0_91]
    at java.net.Socket.connect(Unknown Source) ~[?:1.8.0_91]
    at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57) ~[GangWars-1.0-SNAPSHOT-shaded.jar:?]
    at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[GangWars-1.0-SNAPSHOT-shaded.jar:?]
    ... 3 more

This is my code (happens immediately when the jar runs):

private void loadGangs() {
    MongoDB.getIDs().forEach(id -> GangUtils.addGang(new Gang(id)));
}

My MongoDB class:

public final class MongoDB {

private static final String name = "data";
private static final MongoClient mongoClient = new MongoClient();

public static String getName() {
    return name;
}

public static MongoDatabase getDatabase() {
    return mongoClient.getDatabase(name);
}

public enum CollectionEnum {
    GANG, DRUGS, COPS
}

public static MongoCollection<Document> getCollection(CollectionEnum collection) {
    return getDatabase().getCollection(collection.name().toLowerCase());
}

public static List<Integer> getIDs() {
    List<Document> gangs = getCollection(CollectionEnum.GANG).find().into(new ArrayList<>());
    List<Integer> ids = new ArrayList<>();

    for (Document gang : gangs) {
        Object object = gang.get("_id");

        if (!(object instanceof Document)) continue;

        List<Document> idTags = (List<Document>) object;

        for (Document id : idTags) {
            ids.add(id.getInteger("_id"));
        }

    }

    return ids;
}

public static int getNextID() {
    int id = 0;
    for (int i = 0; i >= getIDs().size(); i++) {
        if (getIDs().get(i) != i) {
            id = i;
            break;
        }
        id++;
    }
    return id;
}

}

What am I doing wrong? (I tried researching on here, but I couldnt find anything associated with Java to help me with MongoDB)

3
  • what's the os you are using if you are using Linux try to diagnose if the 27017 port of mongodb has opened ie, could you connect to mongodb using command mongo ? Commented Aug 3, 2017 at 14:36
  • Using Windows 10. Commented Aug 3, 2017 at 14:41
  • Check the answer to get a way to diagnose if the port 27017 is opening with telnet. didn't get windows by the hand. Commented Aug 3, 2017 at 14:45

2 Answers 2

4

I know that this post is 9 months but this could help others. I had the same issue: the problem is that I unfortunately had test as scope for the de.flapdoodle.embed.mongo dependency, just let it be default (compile) by commenting the scope solved the problem.

<dependency>
   <groupId>de.flapdoodle.embed</groupId>
   <artifactId>de.flapdoodle.embed.mongo</artifactId>
    <!-- <scope>test</scope>  -->
</dependency>

compile is maven default scope. Dependencies with compile scope are needed to build, test, and run the project. Dependencies with test scope are not needed to build and run the project, but to compile and run the unit tests. So a dependency with test as scope will not be attached to the jar/war result of the build process and the contained classes and other resources are not available when we run the project.
see here for more information about maven scopes

Sign up to request clarification or add additional context in comments.

2 Comments

please add why such a change resolved your error? @Meziane
@deckno Here you are.
1

It seems that your MongoDB instance is not up correctly, you can diagnose either by directly run the command mongo or you could use nc -zv localhost 27017 if you are using a Linux distribution.

If you are using windows, you can try to run telnet 127.0.0.1 27017 to see if the port is opening, note that telent command will not be enabled by default, to enable that you should go to the control panel in software and service, enable telnet client. As a developer, you should always enable that, to diagnose some network issues.

Furthermore, there is an ORM driver We find very usefully and has put in production environment for years which was wrapped the default MongoDB-java-driver maintained by the official vendor it's called morphia

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.