开发者

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

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 th开发者_开发技巧e same legacy database.

Any ideas?

Thanks in advance.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜