私は最近同様の問題を解決しました。私は接続後にトランザクション分離レベルを変更していました。この例はあなたを助けるはずです。アドバイスに基づいていますin the final example on this blog post.
# lib/active_record/application_name.rb
module ActiveRecord
module ApplicationName
def self.included(base)
unless base.respond_to? :establish_connection_with_application_name
base.extend ClassMethods
base.class_eval do
class << self
alias_method_chain :establish_connection, :application_name
end
end
end
end
module ClassMethods
def establish_connection_with_application_name(*args)
result = establish_connection_without_application_name(*args)
::ActiveRecord::Base.connection.execute("set application_name = 'abc';")
result
end
end
end
end
# lib/patches/active_record.rb
require 'active_record/application_name'
class ActiveRecord::Base
include ActiveRecord::ApplicationName
end
# config/initializers/patches.rb
require 'patches/active_record'
これは動作していませんが、そのアイデアはそこにありました。 Activerecordはestablish_connectionを介してinternalyを渡しません。代わりにプールを使用します。 ActiveRecord :: ConnectionAdapters :: ConnectionPoolのnew_connectionメソッドの周りにコードを追加して追加しました。うまくいった。私は時間を見つけることができる場合、私は宝石としていくつかの素敵なコードをリリースします。 – Thierry
ああ、冷たい。私の環境では、Unicornのpost_forkブロックでestablish_connectionを明示的に呼び出しています。しかし、アドバイスをありがとう。 –