azure-cosmosdbspring-data-mongodbmongotemplate

Behavior of transactions when using Bulk Operations from mongoTemplate in spring data mongo interacting with cosmos DB


I am trying to understand if the below code will update the batch using a single transaction.

BulkOperations bulkOps = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, SomeEntity.class);
    for (SomeVO SomeVO : SomeVOList) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(SomeVO.getId()));
        Update update = new Update();
        for (Field field : SomeVO.class.getDeclaredFields()) {
            field.setAccessible(true);
            Object value;
            try {
                value = field.get(SomeVO);
            } catch (IllegalAccessException e) {
                continue;
            }

            if (value != null && !field.getName().equals("id")) {
                update.set(field.getName(), value);
            }
        }
        bulkOps.updateOne(query, update);
    }
    BulkWriteResult result = bulkOps.execute();

Just wanted to understand does

bulkOps.execute() 

handle the update in all or nothing fashion? when using spring-data-mongo with cosmosDB


Solution

  • No, by default, bulkOps.execute() does not handle updates in an all-or-nothing fashion when using Spring Data MongoDB with Cosmos DB. The bulkOps operations are executed individually, and if one update fails, it does not roll back the previous successful updates. This means you might end up with a partial update where some documents are updated, and others are not.

    To achieve an all-or-nothing (transactional) behavior, you need to execute the bulk operations within a transaction.