Ok, I got it. But I had to make a little hack on "retrieve_connection" to
achieve this:
module ActiveRecord
module ConnectionAdapters
class ConnectionHandler
def retrieve_connection(klass) #:nodoc:
pool = retrieve_connection_pool(klass)
raise ConnectionNotEstablished, "No connection pool for #{klass}"
unless pool
conn = Thread.current["connection"] || pool.connection
raise ConnectionNotEstablished, "No connection for #{klass} in
connection pool" unless conn
conn
end
end
end
end
I just modify the "conn = pool.connection" to "conn =
Thread.current["connection"] || pool.connection". This allows me to define
the connection using a custom thread variable. With this hack, my code
looked like this:
# slow query process at background
Thread.new do
Thread.current["connection"] = ActiveRecord::Base.connection_pool.checkout
()
100000.times { User.first.update_attributes(some_field: (rand * 100).to_i)
}
ActiveRecord::Base.connection_pool.checkin(Thread.current["connection"])
end
# more slow query process
100000.times { User.first.update_attributes(some_field_2: (rand * 100).to_i)
}
This meets my needs, but I wonder if there isn't a native method to change
the connection of a thread. The way I've done was so ugly :/
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.