[Mongrel] Threading & database connections.
zedshaw at zedshaw.com
Wed Mar 1 15:23:39 EST 2006
You've pretty much figured it out, but there's a bit more detail.
First, here's how AR does "thread safety". AR creates DB connections
and then puts them in the thread local storage. What this means is if
you run any app with AR then you get one DB connection per thread you
use AR in.
Now, what mongrel does (which is shown by the -n option) is it creates a
small set of work threads to process requests. Initially this is 20.
What you should see is as AR gets used your connections will increase up
to 20. The reason is each worker thread will get it's only copy of the
thread local DB connection from AR.
With your setting to -n 1 you can keep the DB connections down to 2, but
then you don't get any advantage of parallel processing.
So, you've got two choices:
1) Use a -n parameter that's reasonable for your uses but doesn't kill
your performance. If you need to only ever have 2 then do #2.
2) Add ActiveRecord.threaded_connections = false to your environment.rb
What #2 does is tells AR to make one connection and always use that.
I may just add an option to mongrel_rails that sets this for you since
people keep asking about it.
On Wed, Mar 01, 2006 at 04:34:54PM -0000, Jeff Jones wrote:
> I have run the server with the -n 1 Parameter for a while now and it only uses two
> connections (as it should). Because of this I am drawing the following conclusion.
> 1. (Most likely) My code is somehow buggering up ( I am a beginner after all).
> 2. Rails is somehow thinking that there is no DB connection when run from a second thread due to a rails bug.
> 2. Rails is somehow thinking that there is no DB connection when run from a second thread due to the way the MUTEX is coded. (I haven't looked at the source code, being a beginner it probably wouldn't help)
> I will see if I can find out more but this is a personal project so I can only do it at lunchtime.
> Mongrel-users mailing list
> Mongrel-users at rubyforge.org
More information about the Mongrel-users