Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-3856

Database connection error is not notified or notified after server selection timeout.

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Cluster Management
    • None

      I try to connect to not existing database.
      Following code does not return error after 60 sec of running.

              MongoClient client = MongoClients.create();
              MongoDatabase db = client.getDatabase("test");
      
              CountDownLatch latch = new CountDownLatch(1);
              db.listCollectionNames().subscribe(new Subscriber<>() {
                  @Override
                  public void onSubscribe(final Subscription subscription) {
                      System.out.println("onSubscribe is called");
                      subscription.request(Long.MAX_VALUE);
                  }
      
                  @Override
                  public void onNext(final String s) {
                      System.out.println("onNext is called: " + s);
                  }
      
                  @Override
                  public void onError(final Throwable throwable) {
                      System.out.println("onError is called");
                      throwable.printStackTrace();
                      latch.countDown();
                  }
      
                  @Override
                  public void onComplete() {
                      System.out.println("Complete is called");
                      latch.countDown();
                  }
              });
      
              System.out.println("Waiting finished with " + latch.await(60, TimeUnit.SECONDS));
      

      Further it prints on console:

      .. Exception in monitor thread while connecting to server localhost:27017
      org.mongodb.driver.cluster - No server chosen by ReadPreferenceServerSelector ... Waiting for 30000 ms before timing out
      ...
      Wait finished with false
      

      This means that onError() was not called.

      When MongoClient is  created with serverSelectionTimeout option as below:

              MongoClient client = MongoClients.create(MongoClientSettings.builder()
                      .applyToClusterSettings(builder -> builder.serverSelectionTimeout(1, TimeUnit.SECONDS))
                      .build());
      

      Execution result is:

      onSubscribe is called
      ... org.mongodb.driver.cluster - Exception in monitor thread while connecting to server 127.0.0.1:2701 ...
      ... (after 1 sec) ...
      onError is called
      com.mongodb.MongoTimeoutException: Timed out after 1000 ms while waiting for a server ...
      ...
      Wait finished with true
      
      

       

      Both cases are unacceptable for me.

      Database connection error should return immediately.

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            akaplanov@gmail.com Andrew Kaplanov
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: