Problem with ActiveRecord and Cerise

C

Caio Chassot

Hi all,

I'm developing a website using the cerise app server and the
activerecord ORM.

After a period of time (few hours / a day), the site stops responding.
I've tracked down the issue to activerecord. Pages that don't do a db
query via AR respond fine.

What could be hanging activerecord? A threading issue? A db connection
issue?

FYI, I'm currently calling ActiveRecord::Base.establish_connection on a
file which is required by each page handler. Is this the appropriate way
to do it?

thanks
 
D

David Heinemeier Hansson

After a period of time (few hours / a day), the site stops responding.
I've tracked down the issue to activerecord. Pages that don't do a db
query via AR respond fine.

What could be hanging activerecord? A threading issue? A db connection
issue?

Active Record is not thread safe. So if Cerise uses threads to handle
incoming connections that could indeed be your problem. The same issue
arises with WEBrick. Active Record is predominately targetted at
CGI/FCGI/mod_ruby environments where threading is not an issue.
FYI, I'm currently calling ActiveRecord::Base.establish_connection on
a file which is required by each page handler. Is this the appropriate
way to do it?

Each call to establish_connection will create a new database
connection. So if at all possible, you should try to minimize the
number of calls. In FCGI that would be to have the establish_connection
call outside of the each_cgi block. I'm not sure how it works with
Cerise.
--
David Heinemeier Hansson,
http://www.instiki.org/ -- A No-Step-Three Wiki in Ruby
http://www.basecamphq.com/ -- Web-based Project Management
http://www.loudthinking.com/ -- Broadcasting Brain
http://www.nextangle.com/ -- Development & Consulting Services
 
M

Michael Neumann

David said:
Active Record is not thread safe. So if Cerise uses threads to handle
incoming connections that could indeed be your problem. The same issue
arises with WEBrick. Active Record is predominately targetted at
CGI/FCGI/mod_ruby environments where threading is not an issue.

Should be easy to fix by using thread-local variables instead of a class
variable:

module ActiveRecord
class Base
def self.connection
Thread.current['conn'] ||= retrieve_connection
Thread.current['conn']
end
def self.connection=(conn)
Thread.current['conn'] = conn
end
def self.connected?
!Thread.current['conn'].nil?
end
end
end


These overrides should do the trick! But haven't tested it!

Regards,

Michael
 
C

Caio Chassot

Michael said:
Should be easy to fix by using thread-local variables instead of a class
variable:

module ActiveRecord
class Base
def self.connection
Thread.current['conn'] ||= retrieve_connection
Thread.current['conn']
end
def self.connection=(conn)
Thread.current['conn'] = conn
end
def self.connected?
!Thread.current['conn'].nil?
end
end
end

Where retrieve_connection calls establish_connection with the approriate
parameters?

if so, it appears to have worked.

thanks.
 
C

Caio Chassot

David said:
Each call to establish_connection will create a new database connection.
So if at all possible, you should try to minimize the number of calls.
In FCGI that would be to have the establish_connection call outside of
the each_cgi block. I'm not sure how it works with Cerise.

Hi David, great library. Can't wait for the rest of rails.

The appplication persists through requests, so how about keeping the
connection in an application-wide variable?

By doing so, Michael's solution would no longer apply, so perhaps adding
a mutex to the select and execute methods of the adapter would do it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top