mongodbspring-bootazure-cosmosdb-mongoapi

Azure Cosmos DB MongoDB API 4.0 transaction feature not working with `spring-boot-starter-data-mongodb`


I have the cosmos mongodb server 4.0 in placed and also have

MongoTransactionManager bean setup and applied @TransactionalOn the method in my poc like below:


@Configuration
class Config extends AbstractMongoConfiguration {

  @Bean
  MongoTransactionManager transactionManager(MongoDbFactory dbFactory) {
    return new MongoTransactionManager(dbFactory);
  }
}


@Service
class DocumentService {

  private final MongoOperations operations;

  DocumentService(MongoOperations operations) {
    this.operations = operations;
  }

  @Transactional
  void insertDocuments() {

    operations.insert(documentOne);
    operations.insert(documentTwo);

// manually raise error here
    Int error = 1/0

  }
}

What I am expecting is it should not insert any record to the DB until it reach the end of the method without any error. In the snippet above, when I am in the debugging , I can see that each insert is being stored in the DB and when error happened, no rollback is being triggered which is completely not ACID at all.

With the same sample, I am able to get the transaction feature working in a pure MongoDB 4.0 server.

And I cannot find any sample or documentation of Java or spring-boot-data-mongo implementation for the transaction feature.

So my question is:

  1. Does Azure Cosmos DB MongoDB API 4.0 Transaction feature compatible with spring-boot-starter-data-mongodb?
  2. Do we have any sample for Java and spring-boot-starter-data-mongodb for transaction feature?

Dependencies I used:

Spring-boot-starter-parent 2.4.3 and spring-boot-starter-data-mongodb


Solution

  • CosmosDB transactions are not supported for partitioned collections.

    Cite from Microsoft's devblog https://devblogs.microsoft.com/cosmosdb/three-reasons-to-upgrade-to-azure-cosmos-db-api-for-mongodb-4-0/:

    Multi-Document Transactions: Multi-document transactions within an unsharded collection support enables you to group together dependent operations and treat them as one operation, while respecting all ACID semantics.

    If you're OK with a hard limit of 10,000 RUs for your collection, you can make it unshared and use transactions.