Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

ActiveRecord 3.1 does not restore connection inheritance after establish_connection/remove_connection on a model #2820

rsutphin opened this Issue Sep 2, 2011 · 6 comments


None yet
4 participants

rsutphin commented Sep 2, 2011

In ActiveRecord 3.1, if you establish a connection on a model and then later remove it, the model does not revert to using the connection on ActiveRecord::Base. Instead, any future database-related operations throw ConnectionNotEstablished. I've created a minimal test case to demonstrate. This is behavior change from AR 2.3 and AR 3.0.

I'd like to patch this, but I'm not sure how. The cause appears to be in ActiveRecord::ConnectionHandler. Its #remove_connection method deliberately disables the pool instead of removing it from the @connection_pools index. I don't know the motivation for this, so I'm not sure how to go about restoring the old behavior.


jonleighton commented Sep 3, 2011

/cc @tenderlove

Was this change intentional?


tenderlove commented Sep 6, 2011

No, I don't think so. This seems like an unintentional bug.


rsutphin commented Sep 6, 2011

Thanks. I can see two ways to fix it — let me know which way is acceptable and I'll provide a patch:

  • When removing a connection, remove the entry in @connection_pools entirely
  • When looking for a connection, skip entries in the hierarchy which are disconnected

I think the first way is better, since it's more explicit and less likely to introduce odd behavior or mask other bugs. What do you think?


tenderlove commented Sep 6, 2011

Yes, I think the first solution is best too.


rsutphin commented Sep 6, 2011

Patch is pull request #2897.

@rsutphin rsutphin closed this Sep 6, 2011

kir commented Oct 15, 2011

I'm not absolutely sure, if this issue relates to my problem, but since upgrading to Rails 3.1.1 I cannot write tests for absence of access to database like in the following example (and it worked in 3.1.0):

model = Task.first
old_con = ActiveRecord::Base.remove_connection
ActiveRecord::Base.establish_connection old_con

This code always throws an exception from model.content, though it shouldn't.

Any hint how to cure this?


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment