# Change the thread connection

**URL:** <https://discuss.rubyonrails.org/t/change-the-thread-connection/70178>\
**Category:** rubyonrails-core\
**Created:** [January 21, 2015, 10:00pm UTC](https://discuss.rubyonrails.org/t/change-the-thread-connection/70178 "2015-01-21T22:00:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Loureiro](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@Daniel\_Loureiro](https://discuss.rubyonrails.org/u/Daniel_Loureiro)\
**Post date:** [January 21, 2015, 10:00pm UTC](https://discuss.rubyonrails.org/t/change-the-thread-connection/70178/1 "2015-01-21T22:00:29Z")

</div>

How can I change the connection of one thread? I want do that:

# slow query process at background

Thread.new do 100000.times { User.first.update\_attributes(some\_field: (rand \* 100).to\_i) } end

# more slow query process

100000.times { User.first.update\_attributes(some\_field\_2: (rand \* 100).to\_i) }

``

I want that these 2 threads runs asynchronously but I discover that Rails uses one single connection to do this. I had tried the below too, but although Rails create a new connection, the “User” don’t use it:

# slow query process at background

Thread.new do conn = ActiveRecord::Base.connection\_pool.checkout() 100000.times { User.first.update\_attributes(some\_field: (rand \* 100).to\_i) } ActiveRecord::Base.connection\_pool.checkin(conn) end

``

I had tried to set “ActiveRecord::Base.connection = conn” but didn’t work too.

It is possible to each thread have your own connection? How can I set the thread connection?

---

<div class="post-metadata">

**Author:** ![Daniel\_Loureiro](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@Daniel\_Loureiro](https://discuss.rubyonrails.org/u/Daniel_Loureiro)\
**Post date:** [January 23, 2015, 9:48am UTC](https://discuss.rubyonrails.org/t/change-the-thread-connection/70178/2 "2015-01-23T09:48:19Z")

</div>

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 😕
