Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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]
Add Comment
Please, Sign In to add comment