MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

Come posso catturare una MongoSecurityException?

Non è possibile intercettare MongoSecurityException poiché viene generato in un thread in background.

Puoi attendere che un MongoTimeoutException gestisca 'in modo sincrono':

  MongoClientOptions clientOptions = new MongoClientOptions.Builder().serverSelectionTimeout(500).build();
    mongoClient = new MongoClient(serverAddress, Collections.singletonList(credential), clientOptions);
    try {
        String address = mongoClient.getConnectPoint();
        System.out.println(address);
    }catch (Throwable e){
        System.out.println(e);
    }

Oppure puoi implementare un ServerListener e gestirlo in modo asincrono

{ 
MongoClientOptions clientOptions = new MongoClientOptions.Builder().addServerListener(this).build();
mongoClient = new MongoClient(host1, Collections.singletonList(credential), clientOptions);
}

@Override
public void serverDescriptionChanged(ServerDescriptionChangedEvent event) {
    Throwable exception = event.getNewDescription().getException();
    handle(exception);
}