Problem with ActiveRecord and Cerise

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

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

David Heinemeier Hansson wrote:

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.

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

David Heinemeier Hansson wrote:

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.

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?

Michael Neumann wrote:

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.