Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable query cache for lock queries #6985

Merged
merged 1 commit into from Jul 6, 2012

Conversation

sidonath
Copy link
Contributor

@sidonath sidonath commented Jul 6, 2012

Query caching should be disabled for queries asking for a pessimistic lock. Otherwise the lock query may not reach the database, thus breaking the expected functionality.

Fixes #867

@@ -83,6 +83,14 @@ def cache_sql(sql, binds)
result.collect { |row| row.dup }
end
end

def locked?(arel)
if arel.respond_to?(:locked)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there some case where locked is not defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sometimes the arel is actually a String.

Without the check QueryCacheTest#test_cache_does_not_wrap_string_results_in_arrays was failing.

rafaelfranca added a commit that referenced this pull request Jul 6, 2012
@rafaelfranca rafaelfranca merged commit 717aa92 into rails:master Jul 6, 2012
@jjb
Copy link
Contributor

jjb commented Jul 6, 2012

@rafaelfranca - should this also be ported to the rails 3 branch?

@rafaelfranca
Copy link
Member

@jjb I can backport to 3-2-stable, but I want to confirm with @tenderlove first.

rafaelfranca added a commit that referenced this pull request Jul 6, 2012
Disable query cache for lock queries
Conflicts:
	activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb
@rafaelfranca
Copy link
Member

@JBB done at 7adc4f2

@jjb
Copy link
Contributor

jjb commented Jul 6, 2012

whoo hoo!

@nragaz
Copy link
Contributor

nragaz commented Jul 27, 2012

Line 83 causes an NoMethodFound exception when there is a 'locked' scope defined on a model. The respond_to? call returns true but the method is not found.

Perhaps having a scope named 'locked' is a bad idea anyway, but that isn't documented as far as I know.

@ihoka
Copy link

ihoka commented Aug 14, 2012

@nragaz Agreed on naming. Just got bitten by this when upgrading Rails. It has a weird interaction with Draper.

@steveklabnik
Copy link
Member

@ihoka oh?

@ihoka
Copy link

ihoka commented Aug 14, 2012

@steveklabnik yes. Draper uses method_missing and respond_to? to delegate to the model.

The issue I ran into was triggered by calling a method on the wrapped model within a decorator, which internally uses associations (has_one in my case). When doing so, it spits out an "undefined method `locked' for #Class:0x007f82fb067068", like this: https://gist.github.com/7498247d20ecd191d77e.

I spent a bunch of time chasing the cause, and finally ended up renaming my "locked" named scope to something else.

@steveklabnik
Copy link
Member

Roger, okay. So not really something I could have fixed.

@nragaz
Copy link
Contributor

nragaz commented Aug 14, 2012

@steveklabnik I think the point is that suddenly relying on the behaviour of a method named locked to all relations may not be so great, especially since there's no documented list of scope and class method names to avoid. It's going to cause a bunch of weird interactions, because if you redefine locked the query_cache logic is still going to call your redefined method -- and sometimes it'll even still seem to work, but mostly it won't.

@steveklabnik
Copy link
Member

@nragaz in this case I'm speaking as the Draper maintainer, not as someone who works on Rails.

I agree that locked is a pretty common name to be adding to every relation.

@nragaz
Copy link
Contributor

nragaz commented Aug 14, 2012

@steveklabnik Ah, cool. I didn't realize you were the Draper maintainer.

On 2012-08-14, at 1:13 PM, Steve Klabnik notifications@github.com wrote:

@nragaz in this case I'm speaking as the Draper maintainer, not as someone who works on Rails.

I agree that locked is a pretty common name to be adding to every relation.


Reply to this email directly or view it on GitHub.

@smathieu
Copy link

I ran into this exact problem as well and have to rename many scopes. Would it be possible to add something similar to DangerousAttributeError for scopes?

@rafaelfranca
Copy link
Member

One of the possibles solution is add a method locked? as alias to loked here and check arel.respond_to?(:locked?). But I need feedback from @tenderlove if this make sense.

@giddie
Copy link

giddie commented May 2, 2013

Would be nice to get this fixed, or at least find a way to provide a slightly less obscure error message. Just got bitten by this.

@ghost
Copy link

ghost commented Aug 19, 2013

Wasted a bunch of time on this. A better error is definitely needed :(


def locked?(arel)
if arel.respond_to?(:locked)
arel.locked
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI we have a scope called locked and this is blowing up ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Caching of Exclusive Row Lock Requests by ActiveRecord
9 participants