ideanl
I’ve been referring to the Phoenix 1.3 Contexts guide for migrating my project and I notice that Repo.transaction() is never used in the controller actions for get_page! and update_page or delete_page. In my project, I’d like to follow the style of the Phoenix guides, but another contributor insists we must use a transaction to group all Repo calls in a controller.
It seems out of place to use Repo in the controller, but I don’t want to wrap all my Repo calls in one context function that contains the transaction call. If needed, where should I put the transaction call (controller or context), otherwise what is a good argument I can present to my contributors for not using a transaction?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Other Trending Topics
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
cmkarlsson
I’d like some clarification on best practices here as well. I understand the thinking about not having any Repo calls in the controllers (or web part as well). Phoenix is pushing “phoenix is not your application”.
Hence one might argue that if you actually do business logic inside your controller you are breaking that “separation” and all of that part should be moved into the context (or into your application, not web layer)
To help with this separation I tend to think: “what if I want to add another user interface to this application?”. Perhaps an ncurses or native GUI interface. If I had any business logic in the web controllers then I would have to replicate that in each interface I make.
On the other hand I feel there is a thin line between fast pragmatic approach of doing some things in the controller and having a proper separation.
It would help if you can you give a concrete example on what sort of Repo calls are being done in the controller.
ideanl
I completely agree and believe in the separation of business logic to the contexts. This is simple when calling one large context function, but becomes impossible if a sequence of context functions are called (getting a record → updating it) which need a common wrapper (transaction). Even if a common wrapper is not needed, as @cmkarlsson mentioned, it would be nice to separate the sequence of calls to business logic in the case of another user interface.
I would appreciate examples as well for how to best handle such a case. I can only think of either creating large context functions to wrap smaller calls but this easily leads to context bloat as the number of potential combinations of function calls is high.
ryh
Only the grouped operations need to be in transactions though. It’s okay to have a function in your context that does a group of operations. You can even compose those smaller functions into a transaction.
Think of it this way - are you really going to need to do that group of operations outside of the transaction? And if that does happen, what will you do? Either remove the transaction or split things out to one that uses a transaction and one that doesn’t. I’ve spent time thinking about things like this and determined that it’s not worth worrying about until the time comes that it’s a problem.
Remember all the other things that you have wrapped in Repo calls (
Repo.allRepo.getetc), why are these okay but notRepo.transaction?bobbypriambodo
What’s their reasoning for insisting that?
Transactions are used for the atomicity of the operation, not necessarily for “grouping”. If you would need to ensure that the operation is atomic, i.e. either all successful or all fails, then all the more reason to wrap that logic in the context function instead of doing it one-by-one at controller site. For all the controller knows, it’s doing one operation, or errors altogether.
This doesn’t necessitate transactions. In this particular case, there’s no added benefit of using a transaction instead of multiple Repo calls.
Note that select calls wrapped in a transaction is valid, but pretty much useless. You would need a transaction when you have a sequence of inserts/updates/deletes, and each of those operations does not make sense on their own.
kip
Depends on the required isolation level. If you lock for update in the select, or you set the isolation level to repeatable read or serializable, you’d probably want the select and update in a transaction to close out the update cycle or recover from locking or timeouts.
bobbypriambodo
I didn’t know that, thanks for the correction. But that use case seems to be too specific to be generalized. It depends on what OP is trying to achieve, but I think in most CRUD cases you wouldn’t need it, no?
cmkarlsson
Well, every CRUD caring about correctness should do this to prevent concurrent updates of data. Any records where multiple persons can edit the same data must use “SELECT FOR UPDATE” before writing to the database.
In a crud app:
A common way to do prevent this is to read back the data you are trying to write to make sure it hasn’t changed. You can then compare field by field or by timestamp or whatever. However, here you must use "SELECT FOR UPDATE (or equivalent) otherwise your record may change while you are doing the comparison.
michalmuskala
The situation described is not helped by transactions - or at least not how they are used in a regular application. In order to help the presented situation with transactions, your transactions would need to span over multiple requests, which isn’t really doable in any practical way.
The simplest solution to the presented problem is optimistic locking, which Ecto supports with
Ecto.Changeset.optimistic_lock/3.ideanl
In the simple case of retrieving a record, creating a changeset, and then performing Repo.update(), the worst that could happen by not using a transaction is that Repo.update would raise with no row found, correct? This case would occur if the record is deleted after the Repo.get but before the Repo.update. A transaction would solve this issue, but at the expense of requiring a specific context function to wrap all the steps above (assuming we keep the Repo calls in the context).
I believe that there are definitely many cases where the lack of a transaction could result in issues with the data, but in this case it helps insignificantly. However, the other contributor to my project still wants a transaction as a matter of principle. I suppose as of now I’ll just wrap the various steps in a context function that uses a transaction.
cmkarlsson
It is only the “read, check, save” requests that needs transactions. Not the initial data fetching.
Yes, I didn’t know about Ecto optimistic locking. I assume the implementation runs in a transaction and uses some sort of read for update lock in the database.
I.e if you don’t lock the current data between your read and update you are open to data races.
I wasn’t aware optimistic locking but principal is the same if you do it manually.