Skip to content

TransactionScope

The TransactionScope class is used to manage and control the lifecycle of a transaction. It provides methods to either commit or roll back changes made during the transaction. Use TransactionScope when you want to perform multiple data/document changes in one transaction.

Methods:

  • commit()
    Commits the current transaction, making all changes made within the transaction permanent. Once a transaction is committed, it cannot be rolled back.
  • rollback()
    Rolls back the current transaction, undoing all changes made during the transaction. This is typically used in case of errors or failures within the transaction to revert the system to its previous state.
Example
// Both Contracts or none will be saved to the storage.   
const transaction = new TransactionScope();  
try {  
   var doc1 = new Contract();  
   doc1.title = "Purchase of Mercedes-Benz 400E";  
   doc1.price = 92423.99;  
   doc1.currency = "USD";  
   doc1.date = new Date;  
   doc1.save();

   var doc2 = new Contract();  
   doc2.title = "Car Insurance";  
   doc2.price = 2023.99;  
   doc2.currency = USD;  
   doc2.date = new Date;  
   doc2.save();

   transaction.commit();    
} catch (error) {  
   transaction.rollback();  
}