Transactions

DianaDb supports transactions. A transaction provides atomicity at the level of a group of operations. Transactions in DianaDb use document-level (row-level) locking, and there are a few things to consider:
  • Transactions have a higher priority than regular requests and can overwrite previously saved data
  • Throughout the execution, all affected documents (updated or removed) are dynamically locked until the transaction is committed or rolled back.
  • Each transaction has its own scope, and changes made inside it are not visible from the outside until you commit it
  • A commit validates every collection first and applies nothing if one fails, so a failed commit leaves the data unchanged
await dianaDb.connect(5000);
// The database the transaction runs in, and the maximum time before it is rolled back automatically
const transactionId: string = await dianaDb.startTransaction({ database: 'test', autoRollBackAfterMs: 60000 });
const manageTransactionParameters = { database: 'test', transactionId };
try {
  await userModel.remove([{ ...definedFilters }], transactionId);
  await dianaDb.commitTransaction(manageTransactionParameters); // { transactionId, status: 'committed' }
} catch (e) {
  await dianaDb.rollbackTransaction(manageTransactionParameters); // { transactionId, status: 'rolled-back' }
}
  
  • startTransaction resolves to the transaction id string.
  • Once a transaction is committed or rolled back its id is finished: any further request with it, a second commit or rollback included, is an error.
  • Documents a transaction updates or removes are locked until it ends: other requests that update or remove them fail with 111 Lock Error, reads are not affected. In a cluster the lock holds on every server (since 4.7.13).
  • A read inside a transaction sees the transaction's own changes, also the result of update operators such as $add.
  • autoRollBackAfterMs must lie between the server's transactions_min_auto_rollback_value and transactions_max_auto_rollback_value (defaults 1000 and 60000 ms); other values are rejected. Without it the maximum is used.
  • Transactions need write access to the database.