How to manage / close multiple db connections inside a ruby ​​roll on rails?

I have a scenario in which models use different database connections.

#database.yml    
development:
    adapter: mysql2
    database: dev_schema
    username: root
    password: root
    host: localhost

otherdb:
    adapter: mysql2
    database: xyz_schema
    username: root
    password: root
    host: localhost

Two models using the above compounds.

   class Foo < ActiveRecord::Base
     #default uses developement connection
   end

   class Bar < ActiveRecord::Base
     establish_connection :otherdb
   end

In one of my use cases, I manually create a stream as shown below.

 Thread.new do 
    begin
        Foo.last
        Bar.last
    rescue Exception => e
      raise e
    ensure
      ActiveRecord::Base.connection.close if ActiveRecord::Base.connection
    end
 end

It is noted that ActiveRecord :: Base.connection.close , closing the connection only develops not otherdb and the stream under high concurrency, establishing more db connections than the connection pool defined in database.yml.

Is there any syntax like Bar.connection.close to close the otherdb connection manually of the current thread?

+4

Source: https://habr.com/ru/post/1609270/


All Articles