How to prevent race condition when save data on Drift #3254
-
Hello everyone, firstly thank you so much for this awesome persistence database I have some question regarding how to prevent race condition when save data on Drift There's case that i have
This My question, when there's case several service access I already research some way to prevent race condition such as Acquiring a Read Lock on SQLite in here , is Drift also support like this? and I already know there's Thanks in advance |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Yes! You can run multiple statements atomically (with an implicit lock on the database) by using transactions: saveOrder(Order updatedOrder) async {
await database.transaction(() async {
Order? localOrder = await database.managers.orders....;
// Rest of your update logic here
})
} Transactions essentially behave as if all their statements inside ran as one (which also means that no concurrent invocation of |
Beta Was this translation helpful? Give feedback.
-
Thank you so much @simolus3 for the answer , I will try to do it later using transactions 👍 |
Beta Was this translation helpful? Give feedback.
Yes! You can run multiple statements atomically (with an implicit lock on the database) by using transactions:
Transactions essentially behave as if all their statements inside ran as one (which also means that no concurrent invocation of
saveOrder
can cause race conditions).