3

In my app, MongoDB 3.2.4 runs on a custom port, I want to implement logic where my app will try to reach MongoDB on a custom port and if it fails it will use the default 27018 port.

In order to do that I use the following code:

String mongoClientURI = "mongodb://" + DB_SRV_USR + ":" + DB_SRV_PWD + "@" + DB_URL + ":" + DB_PORT_CUS + "/" + dbName;
MongoClientURI connectionString = new MongoClientURI(mongoClientURI);

// enable SSL connection
MongoClientOptions.builder().sslEnabled(true).build();

if (this.mongoClient == null) {
    this.mongoClient = new MongoClient(connectionString);
}

// create database if doesn't exist
MongoDatabase mdb = this.mongoClient.getDatabase(dbName);

try {
    this.mongoClient.getAddress();
} catch (com.mongodb.MongoSocketOpenException e) {
    System.out.println("Switch to default port");
    /*…use default port logic…*/
}

The problem is that this exception is not caught. Although MongoDB throws the following exception:

com.mongodb.MongoSocketOpenException: Exception opening socket at com.mongodb.connection.SocketStream.open(SocketStream.java:63) at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:114) at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:128) at java.lang.Thread.run(Thread.java:745) Caused by: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:589) at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50) at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ... 3 more

my try-catch expression can't catch this exception.

I tried multiple approaches, such as to catch:

  • Exception
  • RuntimeException
  • MongoSocketOpenException
  • MongoException
  • MongoCommandException

none of them doesn't work.

My questions:

  1. How can I check if MongoDB connection is established?

  2. How can catch the exception MongoSocketOpenException?

Mike
  • 14,010
  • 29
  • 101
  • 161
  • I agree with Frankenapps, that sentence is not throwing the exception. I think that it is being thrown when you instantiate your client. Catch the exception there. – RubioRic Apr 10 '16 at 09:52

1 Answers1

1
  1. I use this code to check connection:

    try {
        mongo.getAddress();
    } catch (Exception e) {
        System.out.println("Database unavailable!");
        mongo.close();
        return;
    }
    
  2. Not sure here my guess would be that this.mongoClient.getAddress(); does not throw that exception, but I don't really know

EDIT: I initialized it via:

Builder builder = MongoClientOptions.builder().connectTimeout(3000);  
MongoClient mongo = new MongoClient(new ServerAddress("192.168.0.1", 3000), builder.build());
Mike
  • 14,010
  • 29
  • 101
  • 161
frankenapps
  • 5,800
  • 6
  • 28
  • 69
  • What type `mongo` object is? In my case `mongoClient` is a type of `MongoClient`. – Mike Apr 10 '16 at 09:48
  • It looks like we use the same object but different constructor. I use: `String mongoClientURI = "mongodb://" + DB_SRV_USR + ":" + DB_SRV_PWD + "@" + DB_URL + ":" + DB_PORT_CUS + "/" + dbName; MongoClientURI connectionString = new MongoClientURI(mongoClientURI); // enable SSL connection MongoClientOptions.builder().sslEnabled(true).build();` – Mike Apr 10 '16 at 09:55
  • I just tried your approach and the exception was not caught as well. – Mike Apr 10 '16 at 13:10
  • Is there a better way ? – raga Jun 28 '19 at 09:45