Flutter Isar Database — Part 6 — Transactions

In the last article, we dealt with how to do advanced querying. Today we will look at Transactions. Transactions combine multiple database operations in a single unit of work. This is a quite an important topic, so let’s jump right into it.

Happy reading!

If you are new to this series, check out this list.

You can find the whole source code provided in this article here.

What are Transactions?

Transactions are a unit of work that combine multiple data operations. Most of the interactions you do with Isar implicitly use transactions. Read & write access is ACID compliant. If an error occurs, transactions are automatically rolled back.

Transactions are “all or noting”: Either all the actions within a transaction succeed, or none of them take effect. This helps guarantee data consistency.

(Source: https://isar.dev/transactions.html#write-transactions)

Explicit Transactions

Explicit Transactions server you with a consistent snapshot of the database. Because you should try to minimize the time a transaction is active and because of other reasons, it is forbidden to do network calls or other long-running operations in a transaction. In addition, transactions always have a cost. That’s why it’s also important to group successive operations in a single transaction.

Read transactions

It’s optional having explicit read transactions but they allow you to do atomic reads and rely on a consistent state of the database.

You can write Synchronous and Asynchronous read transactions. You should note, that in synchronous transactions you only use synchronous operations, in asynchronous transactions only async operations. This also applies to write transactions. We will discuss them in the next chapter.

  • Synchronous — .txnSync()
  • Asynchronous — .txn()

Note from the documentation itself:

Async read transactions run in parallel to other read and write transactions. Pretty cool, right?

(Source: https://isar.dev/transactions.html#read-transactions)

Write transactions

We have mentioned before, that read transactions are optional, but this is not the case with write transactions. They always have to be wrapped in an explicit transaction. In “What is a transaction” We have mentioned, that Transactions are “all or nothing”. This especially applies to write transactions. After finishing a write transaction successfully, it is automatically committed and all changes are written to the disk. In ace of an error, all changes are discarded and the transaction is aborted.

Note from the documentation itself:

When a database operation fails, the transaction is aborted and must no longer be used. Even if you catch the error in Dart.

(Source: https://isar.dev/transactions.html#write-transactions)

Equally, to read transactions, you can also call write transactions synchronous and asynchronous:

  • Synchronous — .writeTxnSync()
  • Asynchronous — .writeTxn()

Further reading & Conclusion

In this article, you have learned how to use transactions with the local database solution “Isar”.

You can unfold the whole power of Isar if you use packages like Freezed, Riverpod, or Flutter Hooks. If you want to learn these additions, I have entire tutorials about them. Check them out here.

In the following few articles, I will introduce more somewhat complicated packages and explain them (e.g. logger). If you don’t want to miss this, I recommend you to follow me. I tried my best to write the easiest tutorial which everyone understands. If you appreciate this work, I would be very grateful if you could support this quality content and give me some claps!

Thanks for reading, have a nice day!

NOTICE: This article is based on the official Isar documentation. All the information provided here is from this site and a big part of the source code is taken from the documentation. Isar Documentation Source: https://isar.dev/.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tomic Riedel

Tomic Riedel


Flutter, Dart & Appwrite Developer | Livelong learner | Happy about sharing my knowledge