# backgrounDRb versus DRb

**URL:** <https://www.ruby-forum.com/t/backgroundrb-versus-drb/92291>\
**Category:** Rails\
**Created:** [April 17, 2007, 9:57pm UTC](https://www.ruby-forum.com/t/backgroundrb-versus-drb/92291 "2007-04-17T21:57:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [April 17, 2007, 9:57pm UTC](https://www.ruby-forum.com/t/backgroundrb-versus-drb/92291/1 "2007-04-17T21:57:58Z")

</div>

Hey Guys,

I’m using ActiveRecord in a threaded application and getting the “MSQL  
lost its database connection…etc.” error message. Searching through  
the forum I see the issue is basically related to the fact that Rails is  
not thread safe.

In several posting I’ve seen suggestions about using backgrounDRb to  
mitigate this issue. I’ve gone to the backgrounDRb home page and read  
through the project description. I’m still not sure what advantage this  
plugin provides over using DRb … (though I’m not really familiar with  
the advantages of plugins).

The fact that backgrounDRb is in alpha release is also making me a  
little skittish about using it…

Sonny.

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [April 17, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/backgroundrb-versus-drb/92291/2 "2007-04-17T22:20:30Z")

</div>

Hey Guys,

As a follow-up question, I presume that within each backgrounDrb/DRb  
call, ActiveRecord is still not thread-safe. In other words, if I  
attempted to create threads inside \*Drb, I would still run into the same  
'MSQL lost database connection …" error with ActiveRecords right?

Sonny.

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [April 18, 2007, 4:22am UTC](https://www.ruby-forum.com/t/backgroundrb-versus-drb/92291/3 "2007-04-18T04:22:53Z")

</div>

BackgroundDRb makes it easier to leverage DRb and takes care of a lot  
of details for you (assuming you’re not on win32). You can use DRb  
directly, but you’ll have to figure out ways to connect it up to your  
Rails controllers, daemonize a ruby process and keep tabs on currently  
running tasks. All of that isn’t hard, it just takes time away from  
what you really want to accomplish.

It’s still in alpha, but so far it has been solid for me. YMMV

On Apr 18, 3:57 am, Sonny C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [April 18, 2007, 4:24am UTC](https://www.ruby-forum.com/t/backgroundrb-versus-drb/92291/4 "2007-04-18T04:24:54Z")

</div>

Actually, ActiveRecord is thread safe. Set  
ActiveRecord::Base.allow\_concurrency to true in your Backgroun/DRb  
processes.

On Apr 18, 4:20 am, Sonny C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [April 18, 2007, 4:33am UTC](https://www.ruby-forum.com/t/backgroundrb-versus-drb/92291/5 "2007-04-18T04:33:55Z")

</div>

On Apr 17, 2007, at 7:23 PM, eden li wrote:

> Actually, ActiveRecord is thread safe. Set  
> ActiveRecord::Base.allow\_concurrency to true in your Backgroun/DRb  
> processes.

Actually if you set allow\_concurreny to true then you must _always_  
remember to call ActiveRecord::Base.verify\_active\_connections! at  
the end of each worker. Otherwise AR in multi threaded mode uses a  
database connection _per thread_ and never releases them unless you  
call verify\_active\_connections! . If you don’t release the  
connections then you will rapidly use all the database handles your  
database will allow.

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

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [April 18, 2007, 7:43am UTC](https://www.ruby-forum.com/t/backgroundrb-versus-drb/92291/6 "2007-04-18T07:43:29Z")

</div>

Thanks for the good info guys.

I didn’t know ActiveRecord was thread-safe. What could be the cause of  
“MSQL lost its database connection…etc.” error message then? This  
error only occurs in the parent thread after I’ve made an ActiveRecord  
call in the child thread…

Any idea how I can further diagnose the issue would also be appreciated.

Sonny.

> > Actually, ActiveRecord is thread safe. Set  
> > ActiveRecord::Base.allow\_concurrency to true in your Backgroun/DRb  
> > processes.
> 
> Actually if you set allow\_concurreny to true then you must _always_  
> remember to call ActiveRecord::Base.verify\_active\_connections! at  
> the end of each worker. Otherwise AR in multi threaded mode uses a  
> database connection _per thread_ and never releases them unless you  
> call verify\_active\_connections! . If you don’t release the  
> connections then you will rapidly use all the database handles your  
> database will allow.
