title: How to check if MongoDB connection is established with Java?
body: <p>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 <code>27018</code> port.</p>
  
  <p>In order to do that I use the following code:</p>
  
  <pre><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…*/
  }
  </code></pre>
  
  <p>The problem is that this exception is not caught.
  Although MongoDB throws the following exception:</p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>my <code>try-catch</code> expression can't catch this exception.</p>
  
  <p>I tried multiple approaches, such as to catch:</p>
  
  <ul>
  <li><code>Exception</code></li>
  <li><code>RuntimeException</code></li>
  <li><code>MongoSocketOpenException</code></li>
  <li><code>MongoException</code></li>
  <li><code>MongoCommandException</code></li>
  </ul>
  
  <p>none of them doesn't work.</p>
  
  <p><strong>My questions:</strong></p>
  
  <ol>
  <li>How can I check if MongoDB connection is established?</li>
  <li>How can catch the exception <code>MongoSocketOpenException</code>?</li>
  </ol>