Search in sources :

Example 1 with AsyncConnection

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

the class AsyncSingleConnectionBindingTest method shouldReturnTheSameConnection.

@Test
public void shouldReturnTheSameConnection() throws Throwable {
    AsyncConnectionSource asyncConnectionSource = getReadConnectionSource(binding);
    AsyncConnection asyncConnection = getConnection(asyncConnectionSource);
    // Check we get the same connection
    for (int i = 0; i < 100; i++) {
        AsyncConnectionSource connectionSource = getReadConnectionSource(binding);
        AsyncConnection connection = getConnection(connectionSource);
        assertEquals(connection.getDescription().getConnectionId(), asyncConnection.getDescription().getConnectionId());
        connection.release();
        connectionSource.release();
    }
    asyncConnection.release();
    asyncConnectionSource.release();
}
Also used : AsyncConnection(com.mongodb.connection.AsyncConnection) Test(org.junit.Test)

Example 2 with AsyncConnection

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

the class AsyncSingleConnectionBindingTest method shouldHaveTheDifferentConnectionForReadsAndWritesWithNonPrimaryReadPreference.

@Test
public void shouldHaveTheDifferentConnectionForReadsAndWritesWithNonPrimaryReadPreference() throws Throwable {
    AsyncSingleConnectionBinding binding = new AsyncSingleConnectionBinding(getAsyncCluster(), secondary(), ClusterFixture.TIMEOUT, SECONDS);
    AsyncConnectionSource writeSource = getWriteConnectionSource(binding);
    AsyncConnection writeConnection = getConnection(writeSource);
    AsyncConnectionSource readSource = getReadConnectionSource(binding);
    AsyncConnection readConnection = getConnection(readSource);
    assertThat(writeConnection.getDescription().getConnectionId(), is(not(readConnection.getDescription().getConnectionId())));
    writeConnection.release();
    readConnection.release();
    writeSource.release();
    readSource.release();
    binding.release();
}
Also used : AsyncConnection(com.mongodb.connection.AsyncConnection) Test(org.junit.Test)

Example 3 with AsyncConnection

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

the class AsyncSingleConnectionBindingTest method shouldHaveTheSameConnectionForReadsAndWritesWithPrimaryReadPreference.

@Test
public void shouldHaveTheSameConnectionForReadsAndWritesWithPrimaryReadPreference() throws Throwable {
    AsyncConnectionSource writeSource = getWriteConnectionSource(binding);
    AsyncConnection writeConnection = getConnection(writeSource);
    AsyncConnectionSource readSource = getReadConnectionSource(binding);
    AsyncConnection readConnection = getConnection(readSource);
    assertEquals(writeConnection.getDescription().getConnectionId(), readConnection.getDescription().getConnectionId());
    writeConnection.release();
    readConnection.release();
    writeSource.release();
    readSource.release();
}
Also used : AsyncConnection(com.mongodb.connection.AsyncConnection) Test(org.junit.Test)

Example 4 with AsyncConnection

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

the class MixedBulkWriteOperation method executeAsync.

@Override
public void executeAsync(final AsyncWriteBinding binding, final SingleResultCallback<BulkWriteResult> callback) {
    withConnection(binding, new AsyncCallableWithConnection() {

        @Override
        public void call(final AsyncConnection connection, final Throwable t) {
            final SingleResultCallback<BulkWriteResult> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
            if (t != null) {
                errHandlingCallback.onResult(null, t);
            } else {
                validateWriteRequests(connection, bypassDocumentValidation, writeRequests, writeConcern, new AsyncCallableWithConnection() {

                    @Override
                    public void call(final AsyncConnection connection, final Throwable t) {
                        if (t != null) {
                            releasingCallback(errHandlingCallback, connection).onResult(null, t);
                        } else {
                            Iterator<Run> runs = getRunGenerator(connection.getDescription()).iterator();
                            executeRunsAsync(runs, connection, new BulkWriteBatchCombiner(connection.getDescription().getServerAddress(), ordered, writeConcern), errHandlingCallback);
                        }
                    }
                });
            }
        }
    });
}
Also used : AsyncConnection(com.mongodb.connection.AsyncConnection) SingleResultCallback(com.mongodb.async.SingleResultCallback) AsyncCallableWithConnection(com.mongodb.operation.OperationHelper.AsyncCallableWithConnection) BulkWriteBatchCombiner(com.mongodb.connection.BulkWriteBatchCombiner)

Example 5 with AsyncConnection

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

the class OperationHelper method validateWriteRequestCollations.

static void validateWriteRequestCollations(final AsyncConnection connection, final List<? extends WriteRequest> requests, final WriteConcern writeConcern, final AsyncCallableWithConnection callable) {
    Collation collation = null;
    for (WriteRequest request : requests) {
        if (request instanceof UpdateRequest) {
            collation = ((UpdateRequest) request).getCollation();
        } else if (request instanceof DeleteRequest) {
            collation = ((DeleteRequest) request).getCollation();
        }
        if (collation != null) {
            break;
        }
    }
    validateCollationAndWriteConcern(connection, collation, writeConcern, new AsyncCallableWithConnection() {

        @Override
        public void call(final AsyncConnection connection, final Throwable t) {
            callable.call(connection, t);
        }
    });
}
Also used : UpdateRequest(com.mongodb.bulk.UpdateRequest) WriteRequest(com.mongodb.bulk.WriteRequest) AsyncConnection(com.mongodb.connection.AsyncConnection) Collation(com.mongodb.client.model.Collation) DeleteRequest(com.mongodb.bulk.DeleteRequest)

Aggregations

AsyncConnection (com.mongodb.connection.AsyncConnection)6 Test (org.junit.Test)3 SingleResultCallback (com.mongodb.async.SingleResultCallback)1 DeleteRequest (com.mongodb.bulk.DeleteRequest)1 IndexRequest (com.mongodb.bulk.IndexRequest)1 UpdateRequest (com.mongodb.bulk.UpdateRequest)1 WriteRequest (com.mongodb.bulk.WriteRequest)1 Collation (com.mongodb.client.model.Collation)1 BulkWriteBatchCombiner (com.mongodb.connection.BulkWriteBatchCombiner)1 AsyncCallableWithConnection (com.mongodb.operation.OperationHelper.AsyncCallableWithConnection)1