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);
}
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();
}
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;
}
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;
}
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());
}
Aggregations