I was playing around with Sinatra and ActiveRecord when I found that a connections are not reused automatically by this simple API.
#!/usr/bin/env ruby
require 'sinatra'
require 'active_record'
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: 'newsletter.db'
)
ActiveRecord::Schema.define do
create_table :subscribers do |t|
t.string :email
t.timestamps
end
end
class Subscriber < ActiveRecord::Base
validates :email, presence: true
end
class Newsletter < Sinatra::Base
set :server, :thin
get '/subscribers/:email' do
s = Subscriber.find_by_email(params[:email])
if s == nil
status 404
else
content_type 'application/json'
s.to_json
end
end
post '/subscribers/:email' do
Subscriber.create(email: params[:email])
end
end
Newsletter.run!
The API returns either a subscriber or a 404 the first 5 times I GET a subscriber. The 6th time I get a timeout. After each of the first 5 GETs, there is one more read+write file descriptor open for newsletter.db
. I would expect there to be only one all the time.
How do I tell ActiveRecord to reuse a connection?