How to run specific script after connected to oracle using rails?

穿精又带淫゛_ 提交于 2020-01-17 08:17:31

问题


I need to run an oracle script after connect to oracle database using ActiveRecord.
I know that exists the initializers, but these run only in the application's start. I need a point to write a code that runs after every new database connection be established.
This is needed to initialize some oracle environments variables shared with others applications that uses the same legacy database.

Any ideas?

Thanks in advance.


回答1:


I found the solution:
Create the file /config/initializers/oracle.rb and put into it this code:

ActiveRecord::ConnectionAdapters::ConnectionPool.class_eval do
  def new_connection_with_initialization
    result = new_connection_without_initialization
    result.execute('begin Base_Pck.ConfigSession; end;')
    result
  end
  alias_method_chain :new_connection, :initialization
end

The alias_method_chain allows you to change a method (new_connection) without override it, but extending it. Then we need only to change the script into the result.execute call.



来源:https://stackoverflow.com/questions/4683209/how-to-run-specific-script-after-connected-to-oracle-using-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!