# Multi-threaded database interaction

**URL:** <https://www.ruby-forum.com/t/multi-threaded-database-interaction/108766>\
**Category:** Rails\
**Created:** [July 6, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/multi-threaded-database-interaction/108766 "2007-07-06T20:56:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [July 6, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/multi-threaded-database-interaction/108766/1 "2007-07-06T20:56:27Z")

</div>

Hello,

I recently wrote a Rails app that launches 7 threads that each need to  
do ActiveRecord::Base.finds on a PostgreSQL database. I figured that  
since no database information is being changed on calls to find, these  
calls would not require mutual exclusion. However, if I don’t use  
mutual exclusion, I get all sorts of weird errors not long after the  
threads are spawned. It works just fine if I use mutual exclusion.  
Anyone know why this is? Thanks!

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [July 6, 2007, 11:43pm UTC](https://www.ruby-forum.com/t/multi-threaded-database-interaction/108766/2 "2007-07-06T23:43:44Z")

</div>

Matt W. schrieb:

> >

you wouldn’t happen to be sharing variables among threads?

I recently had that problem; I had mistakenly used instance vars in a  
threaded server. Then each new thread would change the instance vars  
and the old thread would go crazy with the new values.

never used the mutual exclusion though, the pick-axe discussion scared  
me away

/ak

---

<div class="post-metadata">

**Author:** ![mwalsh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mwalsh](https://www.ruby-forum.com/u/mwalsh)\
**Post date:** [July 9, 2007, 9:53pm UTC](https://www.ruby-forum.com/t/multi-threaded-database-interaction/108766/3 "2007-07-09T21:53:46Z")

</div>

On Jul 6, 3:42 pm, Andy K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> you wouldn’t happen to be sharing variables among threads?
> 
> I recently had that problem; I had mistakenly used instance vars in a  
> threaded server. Then each new thread would change the instance vars  
> and the old thread would go crazy with the new values.
> 
> never used the mutual exclusion though, the pick-axe discussion scared  
> me away
> 
> /ak

Nope, I was pretty careful about launching each thread with its own  
instance variables.
