Ruby-测试数据库连接是否可能 [英] Ruby - Test whether database connection is possible

查看:79
本文介绍了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.

推荐答案

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

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

这篇关于Ruby-测试数据库连接是否可能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆