4

I have test app written on ruby, using Sinatra+Sequel.

config.ru:

require './main'

run Sinatra::Application

Example code:

require 'sinatra'
require 'haml'
require 'sequel'

DB=Sequel.connect('oracle://test:test@test')

class Tarification < Sequel::Model(DB[:test_table]) 

end

get '/' do
  haml :index
end

Everything was all right until I started using Phusion Passenger in my test environment. Now I've got exception in nginx error.log:

Sequel::DatabaseError - RuntimeError: The connection cannot be reused in the forked process.

Is the right thing to place DB connection routine to rackup file config.ru or it's better to do it in a different way? If the first variant than how to make call to the connection correct from application code?

P.S.: I know that I can do passenger_spawn_method conservative and continue opening connection in app code, but it's not the way I'm looking for because of it's speed and resource usage issues.

Maxim Kachalin
  • 132
  • 1
  • 8
  • In order to answer my own question I've found a bit of related info on [RubyForge](http://rubyforge.org/forum/message.php?msg_id=63321). I had to place `DB.disconnect` at the very bottom of rackup file `config.ru`. But I still think in depth of my mind that it's not overall correct approach. Any direction to sort out how Passenger will manage application would be appreciated. – Maxim Kachalin Jan 15 '12 at 13:16
  • 1
    Passenger doesn't offer a before fork hook that you can use, so calling DB.disconnect at the bottom of config.ru is the recommended approach if you are preloading your app. – Jeremy Evans Jan 17 '12 at 17:17

2 Answers2

2

This issue is documented in Appendix C.3 of the Phusion Passenger manual. The usual method is to hook into the post-fork callback and re-establish the connection there.

Hongli
  • 18,682
  • 15
  • 79
  • 107
1

Sorry, I don't have ability to check it on Passenger and I don't know will it work, but it is more correct to connect in configure block:

configure do
  Sequel.connect(ENV['DATABASE_URL'] || 'sqlite://blog.db')
end

Example is form Scanty — another great Sinatra + Sequel application.

sashaegorov
  • 1,821
  • 20
  • 26