如何绕过ActiveRecord :: StatementInvalid:Mysql2 :: Error:丢失与MySQL的连接

时间:2014-01-10 13:21:22

标签: mysql ruby-on-rails ruby activerecord

我在这里遇到了一个情况。

我想在MySQL服务器关闭时保持站点运行(我的站点应该在DB服务器'mysql'关闭时运行)

我正在使用带有activerecord 4.0.0的MySql2 gem。

据我所知,我需要编写一个中间件来检查数据库服务器是否已启动,如果数据库服务器已启动,那么我们没问题,但如果数据库服务器已关闭,那么我们只需要绕过数据库事务请求和渲染页面/调用(需要捕获MYSQL错误丢失连接异常)。

我知道有多个宝石,如:

所有这些宝石都支持主/从DB概念/体系结构,因为我不想使用主从数据库体系结构。

所以我只是在config / initializer / database_timeout.rb中创建了一个文件,它看起来如下所示。

module Mysql2
  class Client
    def initialize(opts = {})
      opts = Mysql2::Util.key_hash_as_symbols( opts )
      @read_timeout = nil
      @query_options = @@default_query_options.dup
      @query_options.merge! opts

      initialize_ext

      [:reconnect, :connect_timeout, :local_infile, :read_timeout, :write_timeout, :default_file, :default_group, :secure_auth].each do |key|
        next unless opts.key?(key)
        case key
        when :reconnect, :local_infile, :secure_auth
          send(:"#{key}=", !!opts[key])
        when :connect_timeout, :read_timeout, :write_timeout
          send(:"#{key}=", opts[key].to_i)
        else
          send(:"#{key}=", opts[key])
        end
      end

      # force the encoding to utf8
      self.charset_name = opts[:encoding] || 'utf8'

      ssl_options = opts.values_at(:sslkey, :sslcert, :sslca, :sslcapath, :sslcipher)
      ssl_set(*ssl_options) if ssl_options.any?

      if [:user,:pass,:hostname,:dbname,:db,:sock].any?{|k| @query_options.has_key?(k) }
        warn "============= WARNING FROM mysql2 ============="
        warn "The options :user, :pass, :hostname, :dbname, :db, and :sock will be deprecated at some point in the future."
        warn "Instead, please use :username, :password, :host, :port, :database, :socket, :flags for the options."
        warn "============= END WARNING FROM mysql2 ========="
      end

      user     = opts[:username] || opts[:user]
      pass     = opts[:password] || opts[:pass]
      host     = opts[:host] || opts[:hostname]
      port     = opts[:port]
      database = opts[:database] || opts[:dbname] || opts[:db]
      socket   = opts[:socket] || opts[:sock]
      flags    = opts[:flags] ? opts[:flags] | @query_options[:connect_flags] : @query_options[:connect_flags]

      # Correct the data types before passing these values down to the C level
      user = user.to_s unless user.nil?
      pass = pass.to_s unless pass.nil?
      host = host.to_s unless host.nil?
      port = port.to_i unless port.nil?
      database = database.to_s unless database.nil?
      socket = socket.to_s unless socket.nil?

      begin
        connect user, pass, host, port, database, socket, flags
      rescue StandardError => err
        p 'sssssss'
      end
    end
  end
end

现在我在'err'变量中得到以下错误,这意味着我可以成功获得错误异常。请注意,在点击我的网站时会出现此消息。

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

上面的阻止服务器后面的错误

[2014-01-10T18:11:16.080922 #16264]  INFO -- : Started GET "/" for 127.0.0.1 at 2014-01-10 18:11:16 +0500
"sssssss"
"sssssss"
F, [2014-01-10T18:13:53.570439 #16264] FATAL -- : 
Mysql2::Error (closed MySQL connection):
  activerecord (4.0.0) lib/active_record/connection_adapters/mysql2_adapter.rb:77:in `escape'
  activerecord (4.0.0) lib/active_record/connection_adapters/mysql2_adapter.rb:77:in `quote_string'
  activerecord (4.0.0) lib/active_record/connection_adapters/abstract/quoting.rb:15:in `quote'
  activerecord (4.0.0) lib/active_record/connection_adapters/abstract_mysql_adapter.rb:246:in `quote'
  activerecord (4.0.0) lib/active_record/connection_adapters/abstract_mysql_adapter.rb:772:in `block in configure_connection'
  activerecord (4.0.0) lib/active_record/connection_adapters/abstract_mysql_adapter.rb:768:in `each'
  activerecord (4.0.0) lib/active_record/connection_adapters/abstract_mysql_adapter.rb:768:in `map'
  activerecord (4.0.0) lib/active_record/connection_adapters/abstract_mysql_adapter.rb:768:in `configure_connection'
  activerecord (4.0.0) lib/active_record/connection_adapters/mysql2_adapter.rb:265:in `configure_connection'
  activerecord (4.0.0) lib/active_record/connection_adapters/mysql2_adapter.rb:38:in `initialize'
  activerecord (4.0.0) lib/active_record/connection_adapters/mysql2_adapter.rb:20:in `new'
  activerecord (4.0.0) lib/active_record/connection_adapters/mysql2_adapter.rb:20:in `mysql2_connection'
  activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:440:in `new_connection'
  activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:450:in `checkout_new_connection'
  activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:421:in `acquire_connection'
  activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:356:in `block in checkout'
  /home/sannankhalid/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
  activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `checkout'
  activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection'
  /home/sannankhalid/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
  activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection'
  activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in `retrieve_connection'
  activerecord (4.0.0) lib/active_record/connection_handling.rb:79:in `retrieve_connection'
  activerecord (4.0.0) lib/active_record/connection_handling.rb:53:in `connection'
  activerecord (4.0.0) lib/active_record/query_cache.rb:51:in `restore_query_cache_settings'
  activerecord (4.0.0) lib/active_record/query_cache.rb:43:in `rescue in call'
  activerecord (4.0.0) lib/active_record/query_cache.rb:32:in `call'
  activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__4494376046790926309__call__callbacks'
  activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
  actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  airbrake (3.1.14) lib/airbrake/rails/middleware.rb:13:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
  railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
  /home/sannankhalid/.rvm/gems/ruby-1.9.3-p448@astrology-site/bundler/gems/astro_logger-d53871b98caa/lib/astro_logger/middlewares/finish_logging.rb:17:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
  airbrake (3.1.14) lib/airbrake/user_informer.rb:16:in `_call'
  airbrake (3.1.14) lib/airbrake/user_informer.rb:12:in `call'
  railties (4.0.0) lib/rails/engine.rb:511:in `call'
  railties (4.0.0) lib/rails/application.rb:97:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  thin (1.6.1) lib/thin/connection.rb:82:in `block in pre_process'
  thin (1.6.1) lib/thin/connection.rb:80:in `catch'
  thin (1.6.1) lib/thin/connection.rb:80:in `pre_process'
  thin (1.6.1) lib/thin/connection.rb:55:in `process'
  thin (1.6.1) lib/thin/connection.rb:41:in `receive_data'
  eventmachine (1.0.3) lib/eventmachine.rb:187:in `run_machine'
  eventmachine (1.0.3) lib/eventmachine.rb:187:in `run'
  thin (1.6.1) lib/thin/backends/base.rb:73:in `start'
  thin (1.6.1) lib/thin/server.rb:162:in `start'
  rack (1.5.2) lib/rack/handler/thin.rb:16:in `run'
  rack (1.5.2) lib/rack/server.rb:264:in `start'
  railties (4.0.0) lib/rails/commands/server.rb:84:in `start'
  railties (4.0.0) lib/rails/commands.rb:78:in `block in <top (required)>'
  railties (4.0.0) lib/rails/commands.rb:73:in `tap'
  railties (4.0.0) lib/rails/commands.rb:73:in `<top (required)>'
  bin/rails:4:in `require'
  bin/rails:4:in `<top (required)>'
  ruby-debug-ide (0.4.22) lib/ruby-debug-ide.rb:86:in `debug_load'
  ruby-debug-ide (0.4.22) lib/ruby-debug-ide.rb:86:in `debug_program'
  ruby-debug-ide (0.4.22) bin/rdebug-ide:110:in `<top (required)>'
  -e:1:in `load'
  -e:1:in `<main>'

有什么工作吗?或者我应该如何克服我的需求!

帮助真的很感激。感谢。

0 个答案:

没有答案