0Pricing
Elixir & Phoenix: Scalable Backend Development · 강의

트랜잭션과 Ecto.Multi

Repo 트랜잭션과 조합 가능한 Ecto.Multi 구조체를 사용해 여러 데이터베이스 작업을 원자적 단위로 묶는 방법을 배웁니다.

트랜잭션과 Ecto.Multi은(는) CoddyKit의 무료 Elixir & Phoenix: Scalable Backend Development 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Elixir & Phoenix: Scalable Backend Development 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Elixir & Phoenix: Scalable Backend Development 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

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)

Manual Rollback

Inside the function you can abort explicitly with Repo.rollback/1, which returns {:error, reason}.

Repo.transaction(fn ->
  case withdraw(account, 50) do
    {:ok, acc} -> acc
    {:error, r} -> Repo.rollback(r)
  end
end)

The Problem with Nested Functions

Function-based transactions get hard to read and reuse when steps depend on earlier results. Ecto.Multi solves this declaratively.

Building an Ecto.Multi

Ecto.Multi is a struct that accumulates named operations without touching the database until run.

alias Ecto.Multi
multi =
  Multi.new()
  |> Multi.insert(:user, %User{name: "Ada"})

Chaining Operations

Each operation gets a unique name, letting you reference earlier results and inspect them later.

Multi.new()
|> Multi.insert(:user, user_changeset)
|> Multi.insert(:profile, profile_changeset)

Depending on Earlier Steps

Pass a function to use the results of previous steps. It receives the repo and a map of completed operations.

Multi.new()
|> Multi.insert(:user, user_changeset)
|> Multi.run(:welcome, fn _repo, %{user: user} ->
  send_welcome(user)
end)

Running the Multi

Repo.transaction/1 executes the whole Multi atomically and returns {:ok, results} or an error tuple.

case Repo.transaction(multi) do
  {:ok, %{user: user}} -> {:ok, user}
  {:error, step, reason, _} -> {:error, step, reason}
end

Pinpointing Failures

On failure the error tuple names exactly which step failed and gives the changes made so far (which were rolled back).

{:error, :profile, changeset, _changes_so_far}

Other Multi Operations

Multi supports update, delete, update_all, and delete_all, mirroring Repo functions.

Multi.new()
|> Multi.update(:acc, changeset)
|> Multi.delete_all(:logs, old_logs_query)

Composability Wins

Because a Multi is just data, you can build it in pieces across functions and append more steps before running it, improving testability.

def base_multi, do: Multi.new() |> Multi.insert(:user, cs)
# elsewhere
base_multi() |> Multi.insert(:audit, audit_cs) |> Repo.transaction()

Quick Check

Test your transaction knowledge.

Recap

You made database changes atomic:

  • Repo.transaction with a function commits or rolls back together
  • Repo.rollback aborts explicitly
  • Ecto.Multi composes named steps as data
  • Failures return the failing step name and roll back everything

자주 묻는 질문

“트랜잭션과 Ecto.Multi” 강의는 무료인가요?

네 — “트랜잭션과 Ecto.Multi” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Elixir & Phoenix: Scalable Backend Development 강의 전체를 잠금 해제할 수 있습니다. Elixir & Phoenix: Scalable Backend Development 강의에는 총 4개의 강의가 포함되어 있습니다.

“트랜잭션과 Ecto.Multi”에서 뭘 배우나요?

Repo 트랜잭션과 조합 가능한 Ecto.Multi 구조체를 사용해 여러 데이터베이스 작업을 원자적 단위로 묶는 방법을 배웁니다. 브라우저에서 직접 실행하는 실습 코드로 Elixir & Phoenix: Scalable Backend Development을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Elixir & Phoenix: Scalable Backend Development을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Elixir & Phoenix: Scalable Backend Development은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.

“트랜잭션과 Ecto.Multi” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Elixir & Phoenix: Scalable Backend Development 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Elixir & Phoenix: Scalable Backend Development 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. Ecto 스키마와 데이터베이스 마이그레이션
  2. Repo, 변경 집합 및 쿼리
  3. 연관 관계와 내장 스키마
  4. 트랜잭션과 Ecto.Multi
← Elixir & Phoenix: Scalable Backend Development(으)로 돌아가기