views:

586

answers:

2

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.

+1  A: 

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
bb
Thanks, however we've already resolved this the problem in a different manner. I just forgot to close/answer the question *whistles* :)
jimeh
+1  A: 

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.

jimeh