[翻译]  Ruby - Test whether database connection is possible

[CHINESE]  Ruby - 测试数据库连接是否可行


I am looking to implement a way to test the database connection for our application.

我希望实现一种方法来测试我们的应用程序的数据库连接。

We are currently having connectivity issues and therefore want to be able to check connectivity without having to login.

我们目前遇到连接问题,因此希望无需登录即可检查连接。

Is there a way to display a page stating database connection is up or database connection is down dependent upon whether connection fails. If any additional information is needed please let me know.

有没有办法显示一个页面,说明数据库连接已启动或数据库连接已关闭,具体取决于连接是否失败。如果需要任何其他信息,请告诉我。

Only beginning to learn the ropes so apologies if I'm short on detail.

如果我缺乏细节,那就开始学习绳索了。

1 个解决方案

#1


10  

You can check if the connection is possible by running the following script:

您可以通过运行以下脚本来检查是否可以建立连接:

require './config/environment.rb' # Assuming the script is located in the root of the rails app
begin
  ActiveRecord::Base.establish_connection # Establishes connection
  ActiveRecord::Base.connection # Calls connection object
  puts "CONNECTED!" if ActiveRecord::Base.connected? 
  puts "NOT CONNECTED!" unless ActiveRecord::Base.connected?
rescue
  puts "NOT CONNECTED!"
end

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号