Skip to content
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

Spurious query failures with concurrent database connections #69

Closed
stapelberg opened this issue Sep 28, 2024 · 4 comments
Closed

Spurious query failures with concurrent database connections #69

stapelberg opened this issue Sep 28, 2024 · 4 comments

Comments

@stapelberg
Copy link

Thanks for the txdb package, it’s a nice optimization when running many database tests :)

I noticed one issue, though, and wanted to ask if the issue is known or to be expected.

I’m working on a server which maintains multiple caches in the background (i.e. separate goroutines query the database and store the result in memory). My test code starts up a server and makes some database updates/queries itself.

Sometimes, the background goroutine which queries the database reports an error like update: pq: unexpected DataRow in simple query execution and the test itself errors out with pg.Close(): driver: bad connection.

These issues disappear once I add a pg.SetMaxOpenConns(1) call in my test setup.

So I was wondering: Are concurrent database connections supposed to work with txdb?

@flimzy
Copy link
Collaborator

flimzy commented Sep 28, 2024

TL;DR; No, txdb likely won't work with concurrent connections.

Each database transaction is tied to a specific connection. So if your actual connections are shared, that will obviously break txdb.

Setting max open conns to 1 is a good work-around, although that can lead to contention or deadlocks in certain applications.

In your case, where a caching layer is involved, I would try to disable the caching layer during tests, to reduce the number of variables at play in your testing scenario.

@flimzy flimzy closed this as completed Sep 28, 2024
@stapelberg
Copy link
Author

Thanks for confirming!

Setting max open conns to 1 is a good work-around, although that can lead to contention or deadlocks in certain applications.

Yes. Given that Go’s database/sql.Open results in a concurrent setting by default (max open conns defaults to 0, i.e. unlimited), would it make sense to mention pg.SetMaxOpenConns(1) in the README? I couldn’t find any reference to it before reporting this issue.

@flimzy
Copy link
Collaborator

flimzy commented Oct 14, 2024

That probably would be a good addition to the README and/or GoDoc. Feel like submitting a PR?

stapelberg added a commit to stapelberg/go-txdb that referenced this issue Oct 16, 2024
Concurrent transactions are unsupported by txdb.

related to issue DATA-DOG#69
@stapelberg
Copy link
Author

That probably would be a good addition to the README and/or GoDoc. Feel like submitting a PR?

Sure thing: #70

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants