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

What is the reconnect option used for? How can I "reopen" a closed client? #974

Closed
bbuchalter opened this issue May 17, 2018 · 9 comments
Closed

Comments

@bbuchalter
Copy link

Given the following:

config = {
  username: "root",
  password: "",
  host: "localhost",
  reconnect: true
}

myclient = Mysql2::Client.new(config)
myclient.query("SELECT sleep(0) as result")
myclient.close
myclient.query("SELECT sleep(0) as result")

I would expect the reconnect option to allow the connection to be reused after being closed, but instead:

Traceback (most recent call last):
	4: from lib/demo.rb:30:in `<main>'
	3: from /usr/local/lib/ruby/gems/2.5.0/gems/mysql2-0.5.1/lib/mysql2/client.rb:130:in `query'
	2: from /usr/local/lib/ruby/gems/2.5.0/gems/mysql2-0.5.1/lib/mysql2/client.rb:130:in `handle_interrupt'
	1: from /usr/local/lib/ruby/gems/2.5.0/gems/mysql2-0.5.1/lib/mysql2/client.rb:131:in `block in query'
/usr/local/lib/ruby/gems/2.5.0/gems/mysql2-0.5.1/lib/mysql2/client.rb:131:in `_query': MySQL client is not connected (Mysql2::Error)

The use case I was hoping to implement was something similar to File.open when using a block argument, which ensures the file handle is closed when the block terminates.

Similarly, I'd like to ensure my Mysql2 client connections are closed, while avoiding the need for a new Mysql2::Client.new after each call to close. See a simple demo at https://github.com/bbuchalter/mysql2-yield-connect-demo/blob/master/lib/demo.rb.

Perhaps I'm going about this all wrong, but at this point, I'm more curious than anything and would appreciate any insight into the inner workings of this fine open source project. ❤️

@sodabrew
Copy link
Collaborator

sodabrew commented May 17, 2018 via email

@bbuchalter
Copy link
Author

Thanks for the quick reply and explanation. Perhaps you can help me understand why these two cases are handled so differently? I naively assumed that if we can reconnect for an unexpectedly broken connection, we could reconnect from an intentionally closed connection. Thanks again!

@sodabrew
Copy link
Collaborator

sodabrew commented May 17, 2018 via email

@bbuchalter
Copy link
Author

I really appreciate this thoughtful reply. I learned a lot. 👍

@froot
Copy link

froot commented Feb 3, 2021

The reconnect option allows a client whose connection is unexpectedly broken, such as server restart, or network timeout, to reconnect transparently to run the next query.

If the client's connection is unexpectedly broken, will it reconnect and re-run the current query or will it fail the current query then reconnect so that the next query runs with the new connection? @sodabrew

@sodabrew
Copy link
Collaborator

sodabrew commented Feb 3, 2021

db = Mysql2::Client.new...
result = db.query... 

<your code does other things for a few minutes and the connection times out or is interrupted by the network, a server restart, etc.>

result2 = db.query...
<reconnect and issue query!>

The MySQL protocol is stateful. If the connection is idle, that is, not currently processing a query or streaming a result, then it can be reconnected. If the connection was in the middle of a query then the query fails with an error. A connection can be idle but it is still stateful: you might have set a locale, or a timezone, or a session variable, and those are all reset upon reconnect.

More details here: https://dev.mysql.com/doc/c-api/8.0/en/c-api-auto-reconnect.html

@froot
Copy link

froot commented Feb 3, 2021

I see, so in the above example, both queries would run successfully given that the reconnect succeeds. Thanks @sodabrew

@ram-gudapati
Copy link

@sodabrew
In our application we are getting the

WARNING: MYSQL_OPT_RECONNECT is deprecated and will be removed in a future version.
In the documentation I see the following.
Beginning with MySQL 8.0.34, the automatic reconnection feature is deprecated. The related MYSQL_OPT_RECONNECT option is still available but now returns a deprecation warning to the standard error output.
we are using mysql_option MYSQL_OPT_RECONNECT param.
what are alternatives so we get the same behavior without changing our application code to get same behavior as RECONNECT.

@Faq
Copy link

Faq commented Dec 6, 2023

@ram-gudapati #1322

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

5 participants