# ActiveRecord thread-safety

**URL:** <https://www.ruby-forum.com/t/activerecord-thread-safety/97480>\
**Category:** Rails\
**Created:** [July 7, 2006, 11:46am UTC](https://www.ruby-forum.com/t/activerecord-thread-safety/97480 "2006-07-07T11:46:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_BSSS\_rge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@Michael\_BSSS\_rge](https://www.ruby-forum.com/u/Michael_BSSS_rge)\
**Post date:** [July 7, 2006, 11:46am UTC](https://www.ruby-forum.com/t/activerecord-thread-safety/97480/1 "2006-07-07T11:46:25Z")

</div>

Hi,

I want to use ActiveRecord outside Rails. The application is  
multithreaded, so database access has to be thread-safe. I read in a few  
places ActiveRecord in fact should be thread-safe and I gave it a try,  
but my findings indicate ActiveRecord is NOT thread-safe.  
The following test-script fails every with a lost MySQL-connenction:

require ‘rubygems’  
require\_gem ‘activerecord’  
require ‘models/user.rb’

ActiveRecord::Base.establish\_connection(  
:adapter =\> ‘mysql’, :host =\> ‘localhost’,  
:database =\> ‘test’, :username =\> ‘test’  
)

if (!User.find(:first))  
User.create(:name =\> ‘dummy’, :password =\> ‘unsecure’)  
end

threads = []

20.times do |i|  
t = Thread.new do  
User.find(:first)  
end  
threads.push(t)  
end  
threads.each { |t| t.join }

(to reproduce, replace the database and model with something present on  
your system).  
the error message:  
Mysql::Error: Lost connection to MySQL server during query: …

if anybody could shed some light on how thread-safe ActiveRecord really  
is it would be very much appreciated.

Thx,  
Michael

---

<div class="post-metadata">

**Author:** ![Michael\_BSSS\_rge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@Michael\_BSSS\_rge](https://www.ruby-forum.com/u/Michael_BSSS_rge)\
**Post date:** [July 7, 2006, 3:42pm UTC](https://www.ruby-forum.com/t/activerecord-thread-safety/97480/2 "2006-07-07T15:42:09Z")

</div>

On 7/7/06, Michael BÃ¼rge [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I want to use ActiveRecord outside Rails. The application is  
> multithreaded, so database access has to be thread-safe. I read in a few  
> places ActiveRecord in fact should be thread-safe and I gave it a try,  
> but my findings indicate ActiveRecord is NOT thread-safe.

Have you checked the value of ActiveRecord::Base.allow\_concurrency is  
set to true?

Tom

---

<div class="post-metadata">

**Author:** ![Michael\_BSSS\_rge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@Michael\_BSSS\_rge](https://www.ruby-forum.com/u/Michael_BSSS_rge)\
**Post date:** [July 7, 2006, 3:56pm UTC](https://www.ruby-forum.com/t/activerecord-thread-safety/97480/3 "2006-07-07T15:56:28Z")

</div>

Wow, never have seen that before!

It’s not even in the apidocs ([http://api.rubyonrails.org/](http://api.rubyonrails.org/)) but exists…

It there a more accurate documentation sneaking around somewhere?! 🙂

regards  
Peter

Tom W. schrieb:

---

<div class="post-metadata">

**Author:** ![Michael\_BSSS\_rge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@Michael\_BSSS\_rge](https://www.ruby-forum.com/u/Michael_BSSS_rge)\
**Post date:** [July 7, 2006, 6:19pm UTC](https://www.ruby-forum.com/t/activerecord-thread-safety/97480/4 "2006-07-07T18:19:28Z")

</div>

> Have you checked the value of ActiveRecord::Base.allow\_concurrency is  
> set to true?

No I didn’t! …didn’t know it existed.  
It was set to false and when set to true my test works as desired.

That was the missing piece of information needed, thank you very much!

I guess I will look at the sources more from now on after unsuccessful  
searches for documentation/explanation 😉

Michael
