g.verhaag
g.verhaag

Reputation: 139

MongoDB with Eclipse not throwing exception?

I'm trying to use MongoDB with Eclipse in a very basic way, but can't get it working properly!

My code looks like the following (JavaSE-1.6 and mongo-java-drive-2.12.0-rc0.jar):

import java.net.UnknownHostException;

import com.mongodb.MongoClient;

public class MongoDBProjects {

public static void main(String[] args) {

    try {
         MongoClient mongoClient = new MongoClient( "xyz" , 123);
    } catch (UnknownHostException e) {
         e.printStackTrace();
    }
    }

}

This code doesn't show any error in the Eclipse console, but the code should throw an UnknowHostException, because no mongoDB instance is running on xyz and listening on port 123!

Why is that?

Regards, Gerard

Upvotes: 0

Views: 300

Answers (1)

Philipp
Philipp

Reputation: 69663

The MongoClient doesn't create a persistent network connection. It creates a connection when it needs one and closes it again when it doesn't. You won't get a network error message before you actually attempt to do something with a database.

The first thing you will usually do in a real-world application after creating a MongoClient is get a database and authenticate to it. That's the first time you will notice when MongoDB is offline.

Upvotes: 2

Related Questions