ActiveRecord的连接警告。 (数据库连接不会自动关闭)
我试图创建西纳特拉和ActiveRecord的(3.2.3)一个小应用程序。
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:
DE preCATION警告:数据库连接不会被关闭
自动,请关闭您的数据库连接时的结束
线程通过调用关闭
您的连接。例如:
ActiveRecord的:: Base.connection.close
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'
当报警发生时它需要较长时间的页面刷新之前。
我不明白的地方我应该关闭连接。我试图把的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.
更新:的
我忘了提及,我也用西纳特拉/ reloader插件从西纳特拉-contrib请的宝石来看看效果,无需重新启动服务器。
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"
如果我评论它,然后问题消失。但无论如何,我不知道如何摆脱这个问题没有禁用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.
您需要一个中间件添加到您的堆栈。 只是这行添加到您的config.ru机架文件:
You need to add a middleware to your stack. Just add this line to your config.ru rack up file:
use ActiveRecord::ConnectionAdapters::ConnectionManagement
在这里找到了答案: https://github.com/puma/puma/issues/59