Managed and unmanaged transactions
Managed transactions are typically easier for developers with previous object-relational mapping (ORM) experience, and unmanaged transactions may be more familiar to developers who write Structured Query Language (SQL) directly. Unmanaged transactions are explicit by design, but managed transactions have some implicit behaviors for state management such as automatically creating a transaction instance and calling your callback method with that transaction.
Let’s look at the steps for creating unmanaged transactions, as follows:
- We would start by creating a transaction instance, like so:
const tx = await sequelize.transaction();
- Next, we will want to wrap our queries in a
try
block. For this example, we will increment and decrement two account balances by100
using the same transaction instance, like so:try { const amount = 100; await Account.increment( ...