简体   繁体   中英

Right place for Sequel DB connection while working on Phusion Passenger with nginx

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?

PS: 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM