Search in sources :

Example 1 with MongoServerUnavailableException

use of com.mongodb.MongoServerUnavailableException in project mongo-java-driver by mongodb.

the class DefaultConnectionPoolTest method shouldThrowOnPoolClosed.

@Test
public void shouldThrowOnPoolClosed() {
    provider = new DefaultConnectionPool(SERVER_ID, connectionFactory, ConnectionPoolSettings.builder().maxSize(1).maxWaitTime(50, MILLISECONDS).build(), mockSdamProvider());
    provider.close();
    String expectedExceptionMessage = "The server at 127.0.0.1:27017 is no longer available";
    MongoServerUnavailableException exception;
    exception = assertThrows(MongoServerUnavailableException.class, () -> provider.get());
    assertEquals(expectedExceptionMessage, exception.getMessage());
    SupplyingCallback<InternalConnection> supplyingCallback = new SupplyingCallback<>();
    provider.getAsync(supplyingCallback);
    exception = assertThrows(MongoServerUnavailableException.class, supplyingCallback::get);
    assertEquals(expectedExceptionMessage, exception.getMessage());
}
Also used : SupplyingCallback(com.mongodb.client.syncadapter.SupplyingCallback) MongoServerUnavailableException(com.mongodb.MongoServerUnavailableException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with MongoServerUnavailableException

use of com.mongodb.MongoServerUnavailableException in project mongo-java-driver by mongodb.

the class DefaultServer method getConnectionAsync.

@Override
public void getConnectionAsync(final SingleResultCallback<AsyncConnection> callback) {
    if (isClosed) {
        callback.onResult(null, new MongoServerUnavailableException(String.format("The server at %s is no longer available", serverId.getAddress())));
        return;
    }
    SdamIssue.Context exceptionContext = sdam.context();
    operationBegin();
    connectionPool.getAsync(new SingleResultCallback<InternalConnection>() {

        @Override
        public void onResult(final InternalConnection result, final Throwable t) {
            if (t != null) {
                try {
                    operationEnd();
                    sdam.handleExceptionBeforeHandshake(SdamIssue.specific(t, exceptionContext));
                } finally {
                    callback.onResult(null, t);
                }
            } else {
                callback.onResult(AsyncOperationCountTrackingConnection.decorate(DefaultServer.this, connectionFactory.createAsync(result, new DefaultServerProtocolExecutor(), clusterConnectionMode)), null);
            }
        }
    });
}
Also used : SdamIssue(com.mongodb.internal.connection.SdamServerDescriptionManager.SdamIssue) MongoServerUnavailableException(com.mongodb.MongoServerUnavailableException)

Aggregations

MongoServerUnavailableException (com.mongodb.MongoServerUnavailableException)2 SupplyingCallback (com.mongodb.client.syncadapter.SupplyingCallback)1 SdamIssue (com.mongodb.internal.connection.SdamServerDescriptionManager.SdamIssue)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1