Guest User

Untitled

a guest
Oct 25th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. MYSQL_WAIT_TIMEOUT_MAX = 2147483
  2.  
  3. def test_connection(extra_conf = {})
  4. conf = {
  5. :host => "HOST_NAME",
  6. :username => "USER_NAME",
  7. :password => "PASSWORD",
  8. :database => "DATABASE_NAME",
  9. :adapter => "mysql2"
  10. }
  11. ActiveRecord::Base.establish_connection(conf.merge(extra_conf))
  12. puts ActiveRecord::Base.connection.execute("SHOW VARIABLES LIKE \"wait_timeout\"").to_a.join(" : ")
  13. seconds_to_sleep = [ 60, 120, 180, 240, 300, 360]
  14. seconds_to_sleep.each do |seconds|
  15. sleep seconds
  16. puts ActiveRecord::Base.connection.active? ? "Connection up !" : "Connection lost !"
  17. end
  18. end
  19.  
  20. # After more than 5 minutes of inactivity, the ActiveRecord connection becomes disconnected, which results in the "MySQL Server Has Gone Away" error":
  21. irb(main):058:0> test_connection(:wait_timeout => MYSQL_WAIT_TIMEOUT_MAX)
  22. wait_timeout : 2147483
  23. Connection up !
  24. Connection up !
  25. Connection up !
  26. Connection up !
  27. Connection up !
  28. Connection lost !
  29. => [60, 120, 180, 240, 300, 360]
Add Comment
Please, Sign In to add comment