TransactionsΒΆ

A database transaction is a sequence of operations performed as a single logical unit of work. Transactions allow you to roll back all operations should any single operation fail. For example, if you save multiple Dari objects within a transaction, a save exception for one object will prevent all objects in the transactions from being committed to the database. Transactions are thread bound.

As shown in the following code snippet, transactions require an instance of the Database object on which you want to run a transaction. Generally, you want to use the default database.

Every call to beginWrites must be followed by a call to endWrites. The commitWrites API commits all pending writes. The data is either committed to the database, or it is discarded if there are any validation errors thrown in the save operation.

...
Database db = Database.Static.getDefault();
db.beginWrites();
try {
   // Save Dari objects using save().
   object.save();
   if (!rollback) {
      db.commitWrites();
   }
} finally {
     db.endWrites();
  }