Transaction management within bulk write operations

Hello team,

In my springboot application, transactions management does not work in a context using using bulk write operations :

  • Driver : mongodb-driver-sync 4.0.4
  • MongoDb 4.2.9

Please find the code I used below :

private BulkIndices bulkExecution(final List<WriteModel<Document>> requestsList) {

final BulkIndices bulkIndices = new BulkIndices();
if (requestsList.size() == 0) {
  return bulkIndices;
try {
  log.trace("Request list to bulk: {}", requestsList);
  log.trace("[{}] executions to bulk", requestsList.size());
  final Instant start =;

  final ClientSession clientSession = mongoClient.startSession();
  TransactionOptions txnOptions = TransactionOptions.builder()

  TransactionBody txnBody = new TransactionBody<BulkWriteResult>() {
    public BulkWriteResult execute() {
      return productStoreStockCollection.bulkWrite(clientSession, requestsList, DaoUtils.getBulkWriteOptions());

  try {
    // Step 4: Use .withTransaction() to start a transaction, execute the callback, and commit (or abort on error).
    clientSession.withTransaction(txnBody, txnOptions);
    int a = 1/0;  //  Here i just want to add this check to test a transaction  rollback  ..
  } catch (RuntimeException exec) {
    log.error("Operation exception, aborting txn.. {} ", exec);
    throw exec;
  } finally {

  // BulkWriteResult result = productStoreStockCollection.bulkWrite(requestsList, DaoUtils.getBulkWriteOptions());
  final Instant end =;
  log.trace("Bulk of [{}] executions succeed in {}ms", requestsList.size(), ChronoUnit.MILLIS.between(start, end));
  // log.trace("Bulk succeed: {}", result);
} catch (MongoBulkWriteException mongoException) {
  List<BulkWriteError> errorsList = mongoException.getWriteErrors();
  for (BulkWriteError bulkWriteError : errorsList) {
    log.error("Error while processing bulk", mongoException);
return bulkIndices;


Unfortunately, the bulk operation committed the transaction and suddenly the documents were created in the database, and the “division by zero” test did not rollback transaction.

Can you please help me to know why the transaction was committed (documents inserted in database) and why the rollback was not done (disvision on zero).

Thanks in advance.