Skip to content

Instantly share code, notes, and snippets.

@gregblake
Created October 24, 2017 15:58
Show Gist options
  • Select an option

  • Save gregblake/7ec8972c134201184288176600dddff1 to your computer and use it in GitHub Desktop.

Select an option

Save gregblake/7ec8972c134201184288176600dddff1 to your computer and use it in GitHub Desktop.
production_training_timeout.rb
MYSQL_WAIT_TIMEOUT_MAX = 2147483
def test_connection(extra_conf = {})
conf = {
:host => "HOST_NAME",
:username => "USER_NAME",
:password => "PASSWORD",
:database => "DATABASE_NAME",
:adapter => "mysql2"
}
ActiveRecord::Base.establish_connection(conf.merge(extra_conf))
puts ActiveRecord::Base.connection.execute("SHOW VARIABLES LIKE \"wait_timeout\"").to_a.join(" : ")
seconds_to_sleep = [ 60, 120, 180, 240, 300, 360]
seconds_to_sleep.each do |seconds|
sleep seconds
puts ActiveRecord::Base.connection.active? ? "Connection up !" : "Connection lost !"
end
end
# After more than 5 minutes of inactivity, the ActiveRecord connection becomes disconnected, which results in the "MySQL Server Has Gone Away" error":
irb(main):058:0> test_connection(:wait_timeout => MYSQL_WAIT_TIMEOUT_MAX)
wait_timeout : 2147483
Connection up !
Connection up !
Connection up !
Connection up !
Connection up !
Connection lost !
=> [60, 120, 180, 240, 300, 360]
@gregblake
Copy link
Copy Markdown
Author

gregblake commented Oct 24, 2017

On my development environment, the connection remains after 5 minutes of inactivity:

irb(main):023:0> test_connection(:wait_timeout => MYSQL_WAIT_TIMEOUT_MAX)
wait_timeout : 2147483
Connection up !
Connection up !
Connection up !
Connection up !
Connection up !
Connection up !
=> [60, 120, 180, 240, 300, 360]

Here's the same test on demo21:

test_connection(:wait_timeout => MYSQL_WAIT_TIMEOUT_MAX)
wait_timeout : 2147483
Connection up !
Connection up !
Connection up !
Connection up !
Connection up !
Connection up !
=> [60, 120, 180, 240, 300, 360]

And on production:

test_connection(:wait_timeout => MYSQL_WAIT_TIMEOUT_MAXIMUM_VALUE)
wait_timeout : 2147483
Connection up !
Connection up !
Connection up !
Connection up !
Connection up !
Connection up !
=> [60, 120, 180, 240, 300, 360]

@gregblake
Copy link
Copy Markdown
Author

I just tested it again on training-nitro-task1 with the Rails environment training_production, after Johnathan Rodgers made changes related to this bug.

The connection did not time out after more than 5 minutes of inactivity. Here are the results of the test script:

irb(main):025:0> test_connection(:wait_timeout => MYSQL_WAIT_TIMEOUT_MAX)
wait_timeout : 2147483
Connection up !
Connection up !
Connection up !
Connection up !
Connection up !
Connection up !
=> [60, 120, 180, 240, 300, 360]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment