Forum: Ruby on Rails ActiveRecord: using with multiple threads/DB connections

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
34ff26e944c9968496ab6e258cc3dc03?d=identicon&s=25 Max (Guest)
on 2006-01-13 23:47

I'm totally new to Ruby and Rails and I'm trying to set up a prototype
web site. Unfortunately, I'm having problems with ActiveRecord using
multiple threads.

What I want to do is fairly simple. I have a Controller that receives
and saves data from the web in the DB, but once that's done I want to
process this data in another thread and then save the result in some
other DB table. I want to do the processing in another thread because it
may take well over a minute.

When I do this, it works fine as long as I'm not hitting the web server
with additional requests while it is processing. Otherwise, I get all
sorts of errors either DB-related (StatementInvalid) or in rails itself
("nil received when it was not expected") or WEBrick dumps a core.

BTW I'm using MySQL with InnoDB tables. I believe it should work fine
with default settings, but maybe I'm missing something there. I don't
think so though.

I read all the ActiveRecord doc I could find and I believe the problem
is that ActiveRecord globally maps a class to a connection. So there is
no way to use a separate DB connection to do my asynchronous processing.
Since all DB statements (across multiple threads) are executed in the
same connections there is bound to be trouble.

I copy the Controller and some errors I've seen below. You can see what
I mean directly in code.

This seems like a very simple case, I'm sure there is something I'm
doing wrong. What is the 'good' way to do this? Having a separate
process to do the backend processing? But then, what about multiple
threads using ActiveRecord in this backend process?

What am I doing wrong/missing?

I went over the doc many times and couldn't find anything to solve my
problem (I checked ActiveRecord::Base.connection and
.establish_connection, but it doesn't seem like it will help me in this
case.) I read ActiveRecord is thread safe, but that's not what I'm
experiencing. Where does the synchronization take place?

Again, I'm new to RoR so that may be stupid questions. Feel free to send
me a RTFM, but please provide a link to said manual!  ;)



class ArticlesController < ApplicationController
  def create
    @article =[:article])
    if processNewArticle(@article)
      flash[:notice] = 'Article was successfully created.'
      redirect_to :action => 'list'
      render :action => 'new'

  def processNewArticle(article) some validation...
    if (!
    # at this point, we know everything is ok
    # start processing {
      ... some very long processing ...

 ActiveRecord::StatementInvalid in Articles#new

Mysql::Error: Lost connection to MySQL server during query: SHOW FIELDS
FROM articles

RAILS_ROOT: script/../config/..
Application Trace | Framework Trace | Full Trace

./script/../config/../app/controllers/articles_controller.rb:19:in `new'
./script/../config/../app/controllers/articles_controller.rb:19:in `new'

  Mysql::Error in Articles#list

Lost connection to MySQL server during query

RAILS_ROOT: script/../config/..
Application Trace | Framework Trace | Full Trace

34ff26e944c9968496ab6e258cc3dc03?d=identicon&s=25 Max (Guest)
on 2006-01-14 09:43
Digging through the code, I just saw that it is WEBrick that disables

I now manually set allow_concurrency (to true) in my models. Don't know
if this is kosher, but it will suffice for now.
This topic is locked and can not be replied to.