Search in sources :

Example 6 with WriteConcernException

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

the class WriteProtocol method execute.

@Override
public WriteConcernResult execute(final InternalConnection connection) {
    WriteConcernResult writeConcernResult = null;
    RequestMessage requestMessage = null;
    do {
        long startTimeNanos = System.nanoTime();
        RequestMessage.EncodingMetadata encodingMetadata;
        int messageId;
        boolean sentCommandStartedEvent = false;
        ByteBufferBsonOutput bsonOutput = new ByteBufferBsonOutput(connection);
        try {
            if (requestMessage == null) {
                requestMessage = createRequestMessage(getMessageSettings(connection.getDescription()));
            }
            encodingMetadata = requestMessage.encodeWithMetadata(bsonOutput);
            sendStartedEvent(connection, requestMessage, encodingMetadata, bsonOutput);
            sentCommandStartedEvent = true;
            messageId = requestMessage.getId();
            if (shouldAcknowledge(encodingMetadata.getNextMessage())) {
                CommandMessage getLastErrorMessage = new CommandMessage(new MongoNamespace(getNamespace().getDatabaseName(), COMMAND_COLLECTION_NAME).getFullName(), createGetLastErrorCommandDocument(), false, getMessageSettings(connection.getDescription()));
                getLastErrorMessage.encode(bsonOutput);
                messageId = getLastErrorMessage.getId();
            }
            connection.sendMessage(bsonOutput.getByteBuffers(), messageId);
        } catch (RuntimeException e) {
            sendFailedEvent(connection, requestMessage, sentCommandStartedEvent, e, startTimeNanos);
            throw e;
        } finally {
            bsonOutput.close();
        }
        if (shouldAcknowledge(encodingMetadata.getNextMessage())) {
            ResponseBuffers responseBuffers = null;
            try {
                responseBuffers = connection.receiveMessage(messageId);
                ReplyMessage<BsonDocument> replyMessage = new ReplyMessage<BsonDocument>(responseBuffers, new BsonDocumentCodec(), messageId);
                writeConcernResult = getWriteResult(replyMessage.getDocuments().get(0), connection.getDescription().getServerAddress());
            } catch (WriteConcernException e) {
                sendSucceededEvent(connection, requestMessage, encodingMetadata.getNextMessage(), e, startTimeNanos);
                if (writeConcern.isAcknowledged()) {
                    throw e;
                } else if (ordered) {
                    break;
                }
            } catch (RuntimeException e) {
                sendFailedEvent(connection, requestMessage, sentCommandStartedEvent, e, startTimeNanos);
                throw e;
            } finally {
                if (responseBuffers != null) {
                    responseBuffers.close();
                }
            }
        }
        sendSucceededEvent(connection, requestMessage, encodingMetadata.getNextMessage(), writeConcernResult, startTimeNanos);
        requestMessage = encodingMetadata.getNextMessage();
    } while (requestMessage != null);
    return writeConcern.isAcknowledged() ? writeConcernResult : WriteConcernResult.unacknowledged();
}
Also used : WriteConcernResult(com.mongodb.WriteConcernResult) MongoNamespace(com.mongodb.MongoNamespace) BsonDocument(org.bson.BsonDocument) WriteConcernException(com.mongodb.WriteConcernException) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 7 with WriteConcernException

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

the class ProtocolHelper method throwWriteException.

@SuppressWarnings("deprecation")
private static void throwWriteException(final BsonDocument result, final ServerAddress serverAddress) {
    MongoException specialException = createSpecialException(result, serverAddress, "err");
    if (specialException != null) {
        throw specialException;
    }
    int code = WriteConcernException.extractErrorCode(result);
    if (ErrorCategory.fromErrorCode(code) == ErrorCategory.DUPLICATE_KEY) {
        throw new DuplicateKeyException(result, serverAddress, createWriteResult(result));
    } else {
        throw new WriteConcernException(result, serverAddress, createWriteResult(result));
    }
}
Also used : MongoException(com.mongodb.MongoException) WriteConcernException(com.mongodb.WriteConcernException) DuplicateKeyException(com.mongodb.DuplicateKeyException)

Example 8 with WriteConcernException

use of com.mongodb.WriteConcernException in project morphia by mongodb.

the class TestDocumentValidation method createValidation.

@Test
public void createValidation() {
    getMorphia().map(DocumentValidation.class);
    getDs().enableDocumentValidation();
    assertEquals(Document.parse(DocumentValidation.class.getAnnotation(Validation.class).value()), getValidator());
    try {
        getDs().save(new DocumentValidation("John", 1, new Date()));
        fail("Document should have failed validation");
    } catch (WriteConcernException e) {
        assertTrue(e.getMessage().contains("Document failed validation"));
    }
    getDs().save(new DocumentValidation("Harold", 100, new Date()));
}
Also used : DocumentValidation(org.mongodb.morphia.entities.DocumentValidation) Validation(org.mongodb.morphia.annotations.Validation) DocumentValidation(org.mongodb.morphia.entities.DocumentValidation) WriteConcernException(com.mongodb.WriteConcernException) Date(java.util.Date) Test(org.junit.Test)

Aggregations

WriteConcernException (com.mongodb.WriteConcernException)8 Date (java.util.Date)6 Test (org.junit.Test)6 DocumentValidation (org.mongodb.morphia.entities.DocumentValidation)6 Document (org.bson.Document)2 Validation (org.mongodb.morphia.annotations.Validation)2 DuplicateKeyException (com.mongodb.DuplicateKeyException)1 MongoException (com.mongodb.MongoException)1 MongoNamespace (com.mongodb.MongoNamespace)1 MongoWriteException (com.mongodb.MongoWriteException)1 WriteConcernResult (com.mongodb.WriteConcernResult)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 BsonDocument (org.bson.BsonDocument)1 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)1