-
Notifications
You must be signed in to change notification settings - Fork 802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recommended usage of conn and tx #875
Comments
The tx is pinned to the original connection. Technically, you could use the connection after starting a tx but its a bad idea. Pass the tx around instead. But I also suggest that your service methods take neither a Conn or a Tx. You should define the interface that your method needs and accept that. Then your service methods can work with a pool, a connection, or a transaction. |
Impl ex below :
I return a function because I prefer it in my scenario but you see the idea. I can now pass to my insert address a tx or whatever and it will work fine. :) |
Possibly relevant, there have been discussion of adding an interface that allows decoupling of query logic with Txn, Conn in the go source repo. Hopefully they sort out something eventually |
I have a number of service methods which take a pgx.Conn as parameter. In order to run transactions across many services, I am acquiring a single connection from a pool, starting a transaction, and then passing the acquired connection into the service methods to do some logic. Is this the typical way of using this library, or should I be passing the Tx into the service methods?
I guess I’m wondering: is the tx always pinned to the connection that it was started on? If I pass that connection around it’s equivalent to passing around the tx?
The text was updated successfully, but these errors were encountered: