Ruby:捕获Sequel :: DatabaseConnectionError

我使用Docker来运行我的Ruby应用程序,并将MySQL作为数据库。 我需要我的Ruby应用程序等到MySQL完成加载并build立连接。

我使用下面的代码:

def connect_to_db begin puts "Trying to connect to Mysql" Sequel::Model.db = Sequel.connect( // Connection stuff in here ) rescue Sequel::Error => e puts "Mysql connection failed #{e.message}: Retrying." retry end end connect_to_db() 

这运行一次,然后我得到一个错误 – Sequel::DatabaseConnectionError: Mysql2::Error: Unknown MySQL server host (25) – 它不会进入rescue块,不会重试。

我已经尝试了rescue Sequel::DatabaseConnectionError但是这给出了相同的结果。

我需要在这里拯救什么?

得到它与db.test_connection一起工作:

 def connect_to_db_with_mysql_driver begin puts "Trying to connect to Mysql" db = Sequel.connect( // Connection stuff ) if db.test_connection == true Sequel::Model.db = db else raise "Mysql connection failed." end rescue => ex puts ex puts "Retrying..." retry end end 
Interesting Posts