Transactions
Ballerina ORM works with native Ballerina transaction semantics.
Basic transaction
ballerina
transaction {
var createdUser = check db.'from(User).create({
email: "newuser@example.com",
name: "New User"
});
var createdPost = check db.'from(Post).create({
title: "My First Post",
content: "Hello world",
authorId: createdUser["id"]
});
check commit;
}Rollback on error
ballerina
var txResult = trap transaction {
check db.'from(Account)
.'where({id: {equals: 1001}})
.update({balance: 5000});
check db.'from(Account)
.'where({id: {equals: 9999}})
.update({balance: 7000});
check commit;
};
if txResult is error {
// Transaction is rolled back automatically
}Best practices
- validate inputs before entering
transaction {} - use deterministic query filters (
id, unique columns) for update/delete operations - prefer explicit error handling around transaction boundaries
Next step
Continue with Database Support.