4 ms·
The easiest way I have found to avoid this error is simply not to pass the `db` around so much. Every data access function (even seemingly trivial ones) should
by morelisp 6y ago
The easiest way I have found to avoid this error is simply not to pass the `db` around so much. Every data access function (even seemingly trivial ones) should take a `Tx` instead. This also makes your code "composition-ready" if necessary later.
IMO the standard library should provide something that wraps a `Tx` and a `Context` together, as usually I want every statement issued from a transaction bounded in lifetime by the same one I provided to `BeginTx`. This would provide even more incentive to pass around `Tx`s rather than `DB`s, since those functions also often need a context anyway.
- marvinblum 6y agoI agree. I usually have a "model" package abstracting the database access behind functions accepting a transaction. But since I'm using the max open connections for all tests, I can be pretty confident I don't have open transactions somewhere, so I often take the shortcut and just query on "db" directly.