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

Is mysql2 thread-safe? #385

Closed
thehappycoder opened this issue Jun 30, 2013 · 3 comments
Closed

Is mysql2 thread-safe? #385

thehappycoder opened this issue Jun 30, 2013 · 3 comments

Comments

@thehappycoder
Copy link

Please include that info in the documentation.

@sodabrew
Copy link
Collaborator

sodabrew commented Jul 3, 2013

Sort of yes and no, and nothing that's much different than any other mysql client library. It is safe to access mysql2 objects across threads, but there's no locking inside mysql2, and even so, that's secondary to the fact that you cannot do more than one thing at a time with an underlying mysql client socket.

If you need to run queries simultaneously in two different threads, then you should create two separate client objects. Each one will own its own database connection. This is not any different than if you had a single thread and tried to run several queries and stream back the results in an intermingled fashion -- you can't do that. Each result object returned from a client.query will be tied to that Client instance, and that in turn is tied to a mysql connection, which is a stateful protocol so must complete one thing before moving on to another.

@brianmario ought we to put together some explanation of this relationship for the README? I'm happy to write it up when I have some time soon.

@thehappycoder
Copy link
Author

I see. So the standard usage of rails with activerecord + puma + mysql2 has no danger?

@brianmario
Copy link
Owner

@thehappycoder yeah exactly. Things should work fine, but please let us know if not ;)

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

3 participants