TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

The Case Against Ecto.Multi

1 点作者 lobo_tuerto超过 1 年前

1 comment

bhaney超过 1 年前
This is more a case against misusing Multi in simple situations that don&#x27;t call for it at all. If you just want to run a sequence of simple queries in a transaction, then that&#x27;s what passing a function to Repo.transaction is for, and you&#x27;re obviously just adding unnecessary complexity by bundling that kind of thing into a Multi for no reason.<p>If you&#x27;re aggregating a collection of results from remote nodes into a stream that transforms each one as they come in into a group of potentially inter-dependent queries that might not need to run at all, then you might want to reduce over that stream and accumulate into a Multi, then maybe even discard the whole Multi if a critical remote node dies in the middle or the results just aren&#x27;t useful. If you try to use a Repo.transaction function for that, you&#x27;re either wasting database resources by running half a doomed transaction and rolling back, or you&#x27;re hand-rolling a &quot;collection&quot; of yet-to-be-run queries (and essentially reinventing Multi).<p>Multis are specifically for grouping and manipulating a plan of execution. They are not meant to be Ecto&#x27;s general interface to transactions, and of course you&#x27;re going to end up with frequently awkward code if you use them like that.