diff options
author | Damon McCormick + Cameron Walters <github+dm+cw@squareup.com> | 2011-05-10 16:41:05 -0700 |
---|---|---|
committer | Damon McCormick + Cameron Walters <github+dm+cw@squareup.com> | 2011-05-10 16:41:05 -0700 |
commit | 73ca3932d3669f7a6f9bb722a6abaedb75f66929 (patch) | |
tree | 649112f51d651204c71983fe2097363325016a91 /lib | |
parent | a3d9c393b4707a03734dcf6831fb9636fabac045 (diff) | |
download | rails-73ca3932d3669f7a6f9bb722a6abaedb75f66929.tar.gz rails-73ca3932d3669f7a6f9bb722a6abaedb75f66929.tar.bz2 rails-73ca3932d3669f7a6f9bb722a6abaedb75f66929.zip |
Make ToSql more thread safe.
Because the ToSql visitor instance is shared across all threads, there is a
race condition around column types for binary nodes. It's possible, for instance,
to end up with ActiveRecord converting a string value in the final SQL to an
integer during heavy concurrent operations.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 9 |
1 files changed, 4 insertions, 5 deletions
diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 73319364d5..4de497a17c 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -8,14 +8,13 @@ module Arel @engine = engine @connection = nil @pool = nil - @last_column = nil @quoted_tables = {} @quoted_columns = {} end def accept object - @last_column = nil - @pool = @engine.connection_pool + Thread.current[:arel_visitors_to_sql_last_column] = nil + @pool = @engine.connection_pool @pool.with_connection do |conn| @connection = conn super @@ -356,7 +355,7 @@ key on UpdateManager using UpdateManager#key= end def visit_Arel_Attributes_Attribute o - @last_column = column_for o + Thread.current[:arel_visitors_to_sql_last_column] = column_for o join_name = o.relation.table_alias || o.relation.name "#{quote_table_name join_name}.#{quote_column_name o.name}" end @@ -374,7 +373,7 @@ key on UpdateManager using UpdateManager#key= alias :visit_Bignum :literal alias :visit_Fixnum :literal - def quoted o; quote(o, @last_column) end + def quoted o; quote(o, Thread.current[:arel_visitors_to_sql_last_column]) end alias :visit_ActiveSupport_Multibyte_Chars :quoted alias :visit_ActiveSupport_StringInquirer :quoted |