Search in sources :

Example 6 with MongoClientException

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

the class OperationHelper method checkBypassDocumentValidationIsSupported.

static void checkBypassDocumentValidationIsSupported(final AsyncConnection connection, final Boolean bypassDocumentValidation, final WriteConcern writeConcern, final AsyncCallableWithConnection callable) {
    Throwable throwable = null;
    if (bypassDocumentValidation != null && serverIsAtLeastVersionThreeDotTwo(connection.getDescription()) && !writeConcern.isAcknowledged()) {
        throwable = new MongoClientException("Specifying bypassDocumentValidation with an unacknowledged WriteConcern is " + "not supported");
    }
    callable.call(connection, throwable);
}
Also used : MongoClientException(com.mongodb.MongoClientException)

Example 7 with MongoClientException

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

the class ClientSessionImpl method startTransaction.

@Override
public void startTransaction(final TransactionOptions transactionOptions) {
    Boolean snapshot = getOptions().isSnapshot();
    if (snapshot != null && snapshot) {
        throw new IllegalArgumentException("Transactions are not supported in snapshot sessions");
    }
    notNull("transactionOptions", transactionOptions);
    if (transactionState == TransactionState.IN) {
        throw new IllegalStateException("Transaction already in progress");
    }
    if (transactionState == TransactionState.COMMITTED) {
        cleanupTransaction(TransactionState.IN);
    } else {
        transactionState = TransactionState.IN;
    }
    getServerSession().advanceTransactionNumber();
    this.transactionOptions = TransactionOptions.merge(transactionOptions, getOptions().getDefaultTransactionOptions());
    WriteConcern writeConcern = this.transactionOptions.getWriteConcern();
    if (writeConcern == null) {
        throw new MongoInternalException("Invariant violated.  Transaction options write concern can not be null");
    }
    if (!writeConcern.isAcknowledged()) {
        throw new MongoClientException("Transactions do not support unacknowledged write concern");
    }
    clearTransactionContext();
}
Also used : MongoClientException(com.mongodb.MongoClientException) WriteConcern(com.mongodb.WriteConcern) MongoInternalException(com.mongodb.MongoInternalException)

Example 8 with MongoClientException

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

the class DescriptionHelper method createConnectionDescription.

static ConnectionDescription createConnectionDescription(final ClusterConnectionMode clusterConnectionMode, final ConnectionId connectionId, final BsonDocument helloResult) {
    ConnectionDescription connectionDescription = new ConnectionDescription(connectionId, getMaxWireVersion(helloResult), getServerType(helloResult), getMaxWriteBatchSize(helloResult), getMaxBsonObjectSize(helloResult), getMaxMessageSizeBytes(helloResult), getCompressors(helloResult), helloResult.getArray("saslSupportedMechs", null));
    if (helloResult.containsKey("connectionId")) {
        ConnectionId newConnectionId = connectionDescription.getConnectionId().withServerValue(helloResult.getNumber("connectionId").intValue());
        connectionDescription = connectionDescription.withConnectionId(newConnectionId);
    }
    if (clusterConnectionMode == ClusterConnectionMode.LOAD_BALANCED) {
        ObjectId serviceId = getServiceId(helloResult);
        if (serviceId != null) {
            connectionDescription = connectionDescription.withServiceId(serviceId);
        } else {
            throw new MongoClientException("Driver attempted to initialize in load balancing mode, but the server does not support " + "this mode");
        }
    }
    return connectionDescription;
}
Also used : ConnectionDescription(com.mongodb.connection.ConnectionDescription) ConnectionId(com.mongodb.connection.ConnectionId) MongoClientException(com.mongodb.MongoClientException) ObjectId(org.bson.types.ObjectId)

Example 9 with MongoClientException

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

the class ReadConcernHelper method getReadConcernDocument.

public static BsonDocument getReadConcernDocument(final SessionContext sessionContext, final int maxWireVersion) {
    notNull("sessionContext", sessionContext);
    BsonDocument readConcernDocument = new BsonDocument();
    ReadConcernLevel level = sessionContext.getReadConcern().getLevel();
    if (level != null) {
        readConcernDocument.append("level", new BsonString(level.getValue()));
    }
    assertFalse(sessionContext.isSnapshot() && sessionContext.isCausallyConsistent());
    if (sessionContext.isSnapshot() && maxWireVersion < FIVE_DOT_ZERO_WIRE_VERSION) {
        throw new MongoClientException("Snapshot reads require MongoDB 5.0 or later");
    }
    if (shouldAddAfterClusterTime(sessionContext)) {
        readConcernDocument.append("afterClusterTime", sessionContext.getOperationTime());
    } else if (shouldAddAtClusterTime(sessionContext)) {
        readConcernDocument.append("atClusterTime", sessionContext.getSnapshotTimestamp());
    }
    return readConcernDocument;
}
Also used : BsonDocument(org.bson.BsonDocument) MongoClientException(com.mongodb.MongoClientException) BsonString(org.bson.BsonString) ReadConcernLevel(com.mongodb.ReadConcernLevel)

Example 10 with MongoClientException

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

the class LoadBalancedClusterTest method shouldFailSelectServerAsynchronouslyWhenThereIsSRVMisconfiguration.

@Test
public void shouldFailSelectServerAsynchronouslyWhenThereIsSRVMisconfiguration() {
    // given
    String srvHostName = "foo.bar.com";
    ClusterSettings clusterSettings = ClusterSettings.builder().mode(ClusterConnectionMode.LOAD_BALANCED).srvHost(srvHostName).build();
    ClusterableServerFactory serverFactory = mockServerFactory();
    DnsSrvRecordMonitorFactory dnsSrvRecordMonitorFactory = mock(DnsSrvRecordMonitorFactory.class);
    when(dnsSrvRecordMonitorFactory.create(eq(srvHostName), eq(clusterSettings.getSrvServiceName()), any())).thenAnswer(invocation -> new TestDnsSrvRecordMonitor(invocation.getArgument(2)).hosts(Arrays.asList(new ServerAddress("host1"), new ServerAddress("host2"))));
    cluster = new LoadBalancedCluster(new ClusterId(), clusterSettings, serverFactory, dnsSrvRecordMonitorFactory);
    FutureResultCallback<ServerTuple> callback = new FutureResultCallback<>();
    cluster.selectServerAsync(mock(ServerSelector.class), callback);
    MongoClientException exception = assertThrows(MongoClientException.class, callback::get);
    assertEquals("In load balancing mode, the host must resolve to a single SRV record, but instead it resolved to multiple hosts", exception.getMessage());
}
Also used : FutureResultCallback(com.mongodb.async.FutureResultCallback) ClusterSettings(com.mongodb.connection.ClusterSettings) MongoClientException(com.mongodb.MongoClientException) ClusterId(com.mongodb.connection.ClusterId) ServerAddress(com.mongodb.ServerAddress) ServerSelector(com.mongodb.selector.ServerSelector) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Aggregations

MongoClientException (com.mongodb.MongoClientException)19 MongoException (com.mongodb.MongoException)4 BsonDocument (org.bson.BsonDocument)4 MongoInternalException (com.mongodb.MongoInternalException)3 ClusterSettings (com.mongodb.connection.ClusterSettings)3 MongoQueryException (com.mongodb.MongoQueryException)2 MongoSocketException (com.mongodb.MongoSocketException)2 ReadPreference (com.mongodb.ReadPreference)2 RequestContext (com.mongodb.RequestContext)2 ServerAddress (com.mongodb.ServerAddress)2 WriteConcern (com.mongodb.WriteConcern)2 ClusterId (com.mongodb.connection.ClusterId)2 ConnectionDescription (com.mongodb.connection.ConnectionDescription)2 Nullable (com.mongodb.lang.Nullable)2 MongoOperationPublisher.sinkToCallback (com.mongodb.reactivestreams.client.internal.MongoOperationPublisher.sinkToCallback)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 BsonString (org.bson.BsonString)2 BsonValue (org.bson.BsonValue)2 Test (org.junit.jupiter.api.Test)2