0

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.

Douglas Lise
  • 1,466
  • 1
  • 20
  • 46

1 Answers1

0

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.

Douglas Lise
  • 1,466
  • 1
  • 20
  • 46