<p><pre><code> # transaction has been commented out
# with transaction():
db_models = DBAccess.fetch_records(ids)
db_models[0].yo_mama_fat = True
# request ends, data poofs into the ether
</code></pre>
tbh if this doesn't fail either immediately (no open transaction == error on modification attempt) or at GC time (if there's some kind of deferred logic), then I'd say this is an extremely bad framework and it <i>does</i> hold a major part of the blame. "You can mutate database-connected objects and sometimes they save back to the DB, sometimes they do not" is not reasonable behavior.<p>(obviously these frameworks exist. quite a few of them. quantity does not in any way imply sanity.)<p>> <i>2. DO NOT pass DB models in and out of the DB layer.</i><p>Yea, the more I've used "thin" ORMs that give you plain objects, the more I've grown convinced they're the best choice basically all the time. Trying to be magical is cute, but it's guaranteed to be made of spicy unobtanium, and at some point it'll blow up in your face in an extremely convoluted way due to a simple cause that you'll notice is absolutely <i>everywhere</i> and you're just stuck being paranoid forever. There's no need to live like that.
I found the article very hard to follow. Then I read the domain name and it all made sense...<p>Guess our pattern is different, not really felt the pain point the author is talking about. Or it's the language/framework, not sure whatever the example code is written in, but setting properties on entity records would never update the database in the ones I've used.<p>When we did things more manually we'd make sure that methods like `create_main_records` would start a transaction if not in one, and only commit if it started the transaction. This way we could nest without worry. Our database supported nested transactions but using this pattern we didn't feel the need.
Tired: pass live DB model objects around, mutate them to accumulate random changes, call commit() five levels deep, experience the chaos as described by the author.<p>Wired: fetch DB records and construct domain objects, pass them around, do mutation on the outermost level within an explicit transaction. (Typical best practice.)<p>Inspired: "functional core, imperative shell".
> DO NOT blame the framework<p>Why not? Why should I be allowed to db.commit() midway through a transaction?
With great power comes great responsibility.
Well, I don't want either. Give me a safe API!
Yes, and the people who designed that API clearly were not worthy of of the responsibility of providing it
No. Exposing primitives like commit or “begin transaction” isn’t bad or irresponsible design; working with databases is ridiculously hard, which becomes apparent when demand increases.<p>Combining that with spaghetti that does transaction magic at random places guarantees the sort of pain that makes cursing the entire human race seem like a pretty mild response.<p>Higher-level abstractions may prevent some footguns; e.g., an “atomic” decorator/annotation commits automatically after a successful call. They are somewhat easier to understand but come with their own limitations and caveats.
This is like people saying that C is infallible and it’s those stupid lesser-developers that unlike me simply cannot wield its immense power.
No. Usability matters. After all these years software development still has an ‘unfounded male confidence / posturing’ problem and it’s just cringeworthy
I agree. No matter how complicated, or unnecessary, or unintuitive a piece of software or technology is... There is always this contingent of people who pop in and say "It is not that hard!" and furthermore tend to express a view of "I am superior because I figured out this obtuse thing, and you must be inferior because you have not figured it out".<p>I do not know why that mentality exists in the industry, but I see it all the time... for the past 35 years.
It exists in every industry. Listen to anyone in construction, carpenters, electricians, plumbers, etc.
Database engines I've worked with generally support nested transactions, so you can open a transaction, open <i>another</i> transaction, commit it, then roll back the outer transaction... and it's like nothing ever happened.<p>This ability to compose transactions is their main benefit over other kinds of concurrency control!
Giving "I cut my finger with a knife, why is the knife so sharp?" energy. JKJK.<p>I think the option should be there, but it needs to be used responsibly.
Feel like I've solved this non problem like 20 times.<p>If your framework is holding your records in a collection and you keep track of phantoms that have been edited but not saved as they are edited then all this becomes a non issue. That way you do Collection->SaveTransaction or Collection->Save() and the collection does all the db commit, roll back nonsense.<p>Anyway the examples I don't even know if I could call that an ORM. Use a better one.
> # calls helpers that eventually call commit()<p>Show, don't tell. The article should've included an example of these helpers.<p>All in all, this feels poorly written, but also poorly thought out. The DB layer can and should throw an exception if you manually begin/commit inside a context manager. Please <i>do</i> blame it if it doesn't.
This is trivially preventable via affine types
If someone can trivially introduce bad state (or worse, data loss), then the API is clearly broken.
[dead]