Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
813 views
in Technique[技术] by (71.8m points)

ruby - ActiveRecord connection warning. (Database connections will not be closed automatically)

I'm trying to create a little app with Sinatra and ActiveRecord (3.2.3).

This is how my main file looks like:

require "sinatra"
require "sinatra/reloader"
require "active_record"
...

ActiveRecord::Base.establish_connection(
  adapter:  'sqlite3',
  database: 'db.sqlite3',
  host:     'localhost',
)

class Post < ActiveRecord::Base
  ...
end

get('/') { ... }
get('/posts') { ... }
...

It works, but sometimes I get a warning in console:

DEPRECATION WARNING: Database connections will not be closed automatically, please close your database connection at the end of the thread by calling close on your connection. For example: ActiveRecord::Base.connection.close'

When warning occurs it's takes a long time before page refreshes. I don't understand where I should close connection. I've tried to put ActiveRecord::Base.connection.close at the bottom of file, but it doesn't help.

update:

I forgot to mention that I also use sinatra/reloader plugin from sinatra-contrib gem to look at effect without restarting server.

require "sinatra/reloader"

If I comment it out then the problem disappears. But anyway, I'm wondering how to get rid of the problem without disabling reloader.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to add a middleware to your stack. Just add this line to your config.ru rack up file:

use ActiveRecord::ConnectionAdapters::ConnectionManagement

Found the answer here: https://github.com/puma/puma/issues/59


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...