title: Commit and Rollback transactions in MongoDB Java
body: <p>Currently, I am using the following code:-</p>
  <p>(I have marked the two important lines in the code. Between these lines are just some operations on existing documents)</p>
  <pre><code>clientSession.startTransaction();  // Important Line 1.
  Document walletDetailDoc = new Document(&quot;identifier&quot;, &quot;walletBalanceDistribution&quot;);
  Document foundWalletDetailDoc1 = (Document) walletDistributionCollection.find(walletDetailDoc).first();
  walletDetailDoc = new Document(&quot;identifier&quot;, to + &quot;Backup&quot;);
  Document foundWalletDetailDoc2 = (Document) walletDistributionCollection.find(walletDetailDoc).first();
  walletDetailDoc = new Document(&quot;identifier&quot;, from + &quot;Backup&quot;);
  Document foundWalletDetailDoc3 = (Document) walletDistributionCollection.find(walletDetailDoc).first();
  assert foundWalletDetailDoc1 != null;
  assert foundWalletDetailDoc2 != null;
  assert foundWalletDetailDoc3 != null;
  Bson updateWalletDoc = walletDetailDoc;
  Bson updateWalletDocOperation = new Document(&quot;$set&quot;, updateWalletDoc);
  walletDistributionCollection.updateOne(foundWalletDetailDoc1, updateWalletDocOperation);
  updateWalletDoc = new Document(&quot;identifier&quot;, &quot;walletBalanceDistribution&quot;)
          .append(&quot;totalRTKBalanceForPool&quot;, foundWalletDetailDoc2.get(&quot;totalRTKBalanceForPool&quot;))
          .append(&quot;lastCheckedBlockNumber&quot;, foundWalletDetailDoc2.get(&quot;lastCheckedBlockNumber&quot;))
          .append(&quot;lastCheckedTransactionIndex&quot;, foundWalletDetailDoc2.get(&quot;lastCheckedTransactionIndex&quot;))
          .append(&quot;balanceCollectedAsFees&quot;, foundWalletDetailDoc2.get(&quot;balanceCollectedAsFees&quot;));
  updateWalletDocOperation = new Document(&quot;$set&quot;, updateWalletDoc);
  
  //////// TEMPORARY... TO BE REMOVED
  Thread.sleep(5000);
  clientSession.abortTransaction();  // Important Line 2
  boolean abc = true;
  if(abc) {
      return;
  }
  ////////
  </code></pre>
  <p>Now, I excepted that once I call <code>abortTransaction()</code>, the collection would revert back to it's previous state as it was before <code>//Important Line 1</code>. But that doesn't happend. As I keep making operations, they get saved in MonogDB and do not get reverted when <code>// Important line 2</code> completes execution.</p>
  <p>(Just in case, everything is in <code>try catch</code> and there are no errors between any lines. The code successfully reaches the <code>return</code> statement.)</p>
  <p>Also, if required, I am using the following maven dependency:-</p>
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.mongodb&lt;/groupId&gt;
      &lt;artifactId&gt;mongodb-driver-sync&lt;/artifactId&gt;
      &lt;version&gt;4.2.0&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  <p>How to do this operation properly such that everything gets done, or nothing gets done (i.e. rollback/abort).</p>