Video Transcript (English)
Welcome!
In this video, you will learn how to create multi-document transactions in MongoDB for a Node.js application.
A multi-document transaction is a group of database operations that will be completed together as a unit or not at all. They are used when a group of related operations must either all succeed or all fail together, which is a property known as atomicity. Examples of multi-document transactions include transferring money with a mobile payment app, or taking an item from inventory and adding it to a shopping cart in an online shopping cart.
Before we go through an example, let’s review the steps to complete a transaction in a Node.js application.
First, we start a client session. A client session is a logical group or series of operations that should go together. This allows the use of transactions to ensure that all operations in the session are either committed or rolled back. Next, we can define the transaction options, but this step is optional. We then define the sequence of operations to perform inside the transactions. Finally, we release the resources used by the transaction. Connections are expensive resources as well as limited. By closing the session, that connection is released back to the pool. It also protects your data from security vulnerabilities. Keep in mind that, by default, a multi-document transaction has a 60-second time limit. This means that MongoDB will automatically strike down any multi-document transaction that runs for more than 60 seconds.
Let’s go through an example. We will simulate a transaction by using the accounts collection. The transaction involves the transfer of funds from Ahmet’s account to Marcus’s account.
References to the collections, “accounts” and “transfers”, are assigned to a few variables in our code for quick reference. Also, references to the sender, receiver, and transfer amount are stored in the variables account_id_sender, account_id_reciever, and transaction_amount, respectively.
The code for the transaction steps does the following: First, it starts a client session. Then, it defines the sequence of operations to perform inside the transaction. These operations start by subtracting the balance from the first account, then adds that balance to the second account. Every operation in the transaction needs the `session` passed in as an option. Next, the new transfer is inserted into the transfers collection. The transfer_id is then pushed to both the sender’s and receiver’s transfers_complete array. A console log is printed that contains either a success or error message to the console.
If any of the steps in the transaction encounter an error, the transaction is canceled, and none of the changes are saved. Remember that, by default, a transaction has a maximum runtime of 60 seconds.
We end the session and close the client in a `finally` block so that these operations occur whether the transaction is successful or not.
It’s highly recommended that transactions release or close any resources that they use.
Well done!
In this video, you learned how transactions are implemented in MongoDB with Node.js applications for atomic operations spanning multiple documents.