0Pricing
Elixir & Phoenix: Scalable Backend Development · Lesson

Transactions and Ecto.Multi

Group multiple database operations into atomic units using Repo transactions and the composable Ecto.Multi struct.

Why Transactions?

A transaction bundles several operations so they all succeed or all roll back together, keeping data consistent.

Repo.transaction with a Function

The simplest form runs a function; if it returns normally the transaction commits, if it raises it rolls back.

Repo.transaction(fn ->
  Repo.insert!(%Account{balance: 100})
  Repo.insert!(%Log{action: "created"})
end)

All lessons in this course

  1. Ecto Schema and Database Migrations
  2. Repo, Changesets, and Queries
  3. Associations and Embedded Schemas
  4. Transactions and Ecto.Multi
← Back to Elixir & Phoenix: Scalable Backend Development