# Activerecord thread safey outside of rails

**URL:** <https://www.ruby-forum.com/t/activerecord-thread-safey-outside-of-rails/109602>\
**Category:** Rails\
**Created:** [July 17, 2007, 8:16am UTC](https://www.ruby-forum.com/t/activerecord-thread-safey-outside-of-rails/109602 "2007-07-17T08:16:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![snacktime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@snacktime](https://www.ruby-forum.com/u/snacktime)\
**Post date:** [July 17, 2007, 8:16am UTC](https://www.ruby-forum.com/t/activerecord-thread-safey-outside-of-rails/109602/1 "2007-07-17T08:16:00Z")

</div>

Hoping someone could confirm a couple of things for me.

I’m playing with activerecord/activerecord-jdbc inside jruby threads  
with allow\_concurrency set to true. It’s creating a new connection  
for each thread and appears to be working fine. Transactions appear  
to be working ok also and I haven’t seen any queries going into the  
wrong transactions.

Anything else that might be a problem?

Chris

---

<div class="post-metadata">

**Author:** ![snacktime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@snacktime](https://www.ruby-forum.com/u/snacktime)\
**Post date:** [September 25, 2007, 11:02pm UTC](https://www.ruby-forum.com/t/activerecord-thread-safey-outside-of-rails/109602/2 "2007-09-25T23:02:39Z")

</div>

On 7/8/07, Ezra Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> Yeah AR uses one connection per thread in multithreaded mode. You
> 
> ```
> 
> need to call ActiveRecord::Base.verify\_active\_connections! every 20  
> threads or so to clear out old connections. But be careful of AR in  
> supposed thread safe mode, I’ve run into some really nasty issues  
> where one thread gets its result set mixed up with results form  
> another thread and data gets intertwined.

I’ve heard that. If you happen to have any test cases lying around  
that can produce that behavior I’d love to tests it against jruby to  
see if it’s ar that is the problem or ruby itself.

Your best bet is to not use

> use\_db do  
> foo = Foo.find :all  
> foo.bar = 'baz;  
> foo.save  
> end

Ya that works fine, I have a connection pool module that does about  
the same thing. But then it blocks, and it’s actually worse then just  
processing each request synchronously. Instead of the requests  
getting processed in the order they are received, it’s too easy for  
some to ‘race’ ahead of others to the blocking point.

Chris

---

<div class="post-metadata">

**Author:** ![snacktime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@snacktime](https://www.ruby-forum.com/u/snacktime)\
**Post date:** [September 25, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/activerecord-thread-safey-outside-of-rails/109602/3 "2007-09-25T23:03:36Z")

</div>

Hmm found one problem already, database connections are still held  
open even after the thread is gone.

Chris

---

<div class="post-metadata">

**Author:** ![snacktime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@snacktime](https://www.ruby-forum.com/u/snacktime)\
**Post date:** [September 25, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/activerecord-thread-safey-outside-of-rails/109602/4 "2007-09-25T23:03:31Z")

</div>

On Jul 8, 2007, at 2:19 PM, snacktime wrote:

> Hmm found one problem already, database connections are still held  
> open even after the thread is gone.
> 
> Chris

Yeah AR uses one connection per thread in multithreaded mode. You  
need to call ActiveRecord::Base.verify\_active\_connections! every 20  
threads or so to clear out old connections. But be careful of AR in  
supposed thread safe mode, I’ve run into some really nasty issues  
where one thread gets its result set mixed up with results form  
another thread and data gets intertwined. Your best bet is to not use  
it in threaded mode and instead wrap a small mutex around anywhere  
you call AR.

Perhaps something like this:

def use\_db  
@mutex ||= Mutex.new  
@mutex.synchronize { yield }  
end

use\_db do  
foo = Foo.find :all  
foo.bar = 'baz;  
foo.save  
end

Cheers-  
– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)
