w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Active record reset database connection issues - ActiveRecord::Connection.clear_active_connections

First off, I think you want ActiveRecord::Base.connection

I'm looking at these issues now. As best I can tell ActiveRecord::Base.establish_connection will drop all prior connections from the pool and only use the new connection.

I'm running into issues when I'm swapping back and forth and changing connections both at the ActiveRecord::Base level and at the individual model level (i.e. User.establish_connection. Under certain circumstances, I find that if I establish a connection on the model, then establish a connection on ActiveRecord::Base (intending to also use the new connection on the model), the model will retain the connection.

To wit:

$ ActiveRecord::Base.connection_config[:host]
-> main
$ User.connection_config[:host]
-> main

$ User.establish_connection :blah
$ User.connection_config[:host]
-> blah
$ ActiveRecord::Base.connection_config[:host]
-> main

$ ActiveRecord::Base.establish_connection :blah
$ User.connection_config[:host]
-> blah
$ ActiveRecord::Base.connection_config[:host]
-> blah

$ ActiveRecord::Base.establish_connection :main
$ User.connection_config[:host]
-> blah
$ ActiveRecord::Base.connection_config[:host]
-> main

The important point to note here is that, since you've established the connection on the User model explicitly, ActiveRecord knows to use a different connection pool than that used for the other children of ActiveRecord::Base; thus, when you switch the ActiveRecord::Base connection back, it doesn't switch all models, just the ones sharing the main connection pool.

Here's the ConnectionHandler doc: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionHandler.html





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.