Reputation: 1411
We are using Sinatra and Sequel for a small API implementation. The problem we have however is that on every page request Sequel opens new connections to MySQL, and keeps them open till they timeout, or you restart Apache.
There's not a lot of documentation on how to reuse connections, so any help, explanations, and/or pointers in the right direction would help.
Upvotes: 2
Views: 1861
Reputation: 1411
We figured out what we were doing wrong. It was rather stupid, we initialized Sequel in a before filter in Sinatra.
So instead we do:
DB = Sequel.mysql("...")
Then we simply use the DB
constant to use Sequel.
Upvotes: 1
Reputation: 1400
I wrapped the Sequel stuff in a tiny wrapper and reuse this wrapper, like this:
get '/api/:call' do
@@api ||= SApi.new
@@api.call(params[:call])
end
class SApi
def initialize
connect
end
def connect
@con = Sequel.connect("...")
end
def call(x)
#handle call using @con
end
end
Alternatively, you can call @con.disconnect once you're finished or call Sequel.connect using a block:
Sequel.connect("...") do |c|
# work with c
end #connection closed
Upvotes: 1