3
votes

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?
1
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

1 Answers

1
votes
  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());