Search code examples
mongodbcommitspring-data-mongodb

Default Commit behavior of MongoOperations without explicit Transaction Support


What is the default commit behavior in spring-data-mongodb, if one uses @Autowired MongoOperations without using @Transactional and without explicit MongoTransactionManager Bean initialization in code?

We can see if the MongoTxnMgr is used along with @Transaction support, autocommit:false is visible in org.mongodb.driver.protocol.command in the logs followed by an explicit commitTransaction afterwards;

However when it is not used, autocommit is absent from the same command. In such scenario, when exactly the commit takes place?

We are using Mongo 4.X Server, spring-data-mongodb 2.2.5 RELEASE


Solution

  • The MongoDB 4.X behavior from a client perspective using an explicit transaction mechanism or without is clarified in this discussion

    Excerpt - For MongoDB 4.X

    for a user you should consider that any write outside of the transaction is autocommited by default. So each atomic write is either fully stored or discarded.