Transaction
- Dart course
- Advanced
- Lesson 46/83
- 5 min
Learn atomic operations with Transaction.
Transaction
A transaction runs multiple database operations as a single atomic unit. Either all succeed or none. On error the whole operation is rolled back; on success it commits.
In sqflite you start a transaction with await db.transaction((txn) async { ... }). Inside the block you use txn.insert, txn.query and similar. Example: a money transfer — deduct from one account, add to another; if one fails both are rolled back.
An atomic transfer example
Consider a bank transfer: deduct 100 lira from account A, add 100 lira to account B. These two operations are one unit; if one fails the other must roll back. Otherwise money vanishes or duplicates.
db.transaction((txn) async { await txn.insert('accounts', {'id': 1, 'amount': 100}); await txn.insert('accounts', {'id': 2, 'amount': 50}); print('commit'); }). If both inserts succeed, 'commit' is printed. If one throws, none commits. In this lesson we apply this pattern.
Practice An atomic transfer
The code below has main open a bank.db. Define transferDemo(Database db): inside a db.transaction((txn) async { ... }) block add two txn.insert('accounts', ...) calls (id 1 amount 100 and id 2 amount 50) and at the end of the block call print('commit'). main already has print('Transaction: atomic transfer'); add the transferDemo(db) call.
Starter code
import 'package:sqflite/sqflite.dart';
Future<void> transferDemo(Database db) async {
// make two inserts atomic with db.transaction
// print 'commit' on success
}
void main() async {
print('Transaction: atomic transfer');
final db = await openDatabase('bank.db');
await transferDemo(db);
await db.close();
}Sample solution
import 'package:sqflite/sqflite.dart';
Future<void> transferDemo(Database db) async {
await db.transaction((txn) async {
await txn.insert('accounts', {'id': 1, 'amount': 100});
await txn.insert('accounts', {'id': 2, 'amount': 50});
print('commit');
});
}
void main() async {
print('Transaction: atomic transfer');
final db = await openDatabase('bank.db');
await transferDemo(db);
await db.close();
}Expected output
Transaction: atomic transfer commit
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does a transaction provide?
- Faster queries
- Atomicity Correct answer
- Less memory
- Auto backup
Explanation: a transaction provides atomicity — all or nothing.
2 Match the transaction parts
| Term | Meaning |
|---|---|
db.transaction |
Atomic block |
txn.insert |
In-block insert |
commit |
Successful end |
rollback |
Rolls back |
3 The db.transaction connector
await db.____((txn) async {
await txn.insert('accounts', {'id': 1, 'amount': 100});
await txn.insert('accounts', {'id': 2, 'amount': 50});
print('commit');
});
await db.transaction((txn) async {
await txn.insert('accounts', {'id': 1, 'amount': 100});
await txn.insert('accounts', {'id': 2, 'amount': 50});
print('commit');
});
Explanation: db.transaction starts the atomic block.