替换 ActiveRecord 5 中的 ActiveRecord::ConnectionAdapters::ConnectionManagement
Replacing ActiveRecord::ConnectionAdapters::ConnectionManagement in ActiveRecord 5
我们正在将 Sinatra application from ActiveRecord 4 升级到 ActiveRecord 5。之前我们有这一行:
use ActiveRecord::ConnectionAdapters::ConnectionManagement
这是因为请求完成后连接没有被清理。这是关于此主题的先前 SO 讨论:
- ActiveRecord connection warning. (Database connections will not be closed automatically)
- ActiveRecord::ConnectionTimeoutError
从 ActiveRecord 5 开始,此行不再有效。这个conversation in the rails project states:
This was removed in favor of Executor and Reloader APIs. That
middleware that was removed were not part of the public API. If you
want to use that outside Rails you need to make one.
这是否意味着,如果有人要将 ActiveRecord 5 与 Sinatra 一起使用,连接将再次 'leaked' 或在请求后不返回到池中,除非开发人员重新创建现在-删除中间件?
在 Sinatra 示例中,我们现在是否需要在 ActiveRecord 5 中包含这一行?
after do
ActiveRecord::Base.clear_active_connections!
end
这就是链接线程的含义,但我想得到一个明确的答案,我可以带回我的开发团队。
你是对的,ConnectionManagement
中间件已从 ActiveRecord 5 (PR #23807) 中删除,因此在 Rails 之外设置 ActiveRecord 时需要复制类似的功能.有几种方法可以做到这一点:
1。 ConnectionManagement
机架中间件
ConnectionManagement
class不是很复杂。您可以将实现复制并粘贴到本地应用程序的某处,并像往常一样将其包含到您的 Rack 中间件堆栈中:
class ConnectionManagement
def initialize(app)
@app = app
end
def call(env)
testing = env['rack.test']
status, headers, body = @app.call(env)
proxy = ::Rack::BodyProxy.new(body) do
ActiveRecord::Base.clear_active_connections! unless testing
end
[status, headers, proxy]
rescue Exception
ActiveRecord::Base.clear_active_connections! unless testing
raise
end
end
use ConnectionManagement
2。 (特定于 Sinatra)连接管理 after
挂钩
在 Sinatra 应用程序中,您建议的块应该有效:
after do
ActiveRecord::Base.clear_active_connections!
end
注意这也是方法currently used by the sinatra-activerecord
integration gem to support ActiveRecord 5 (see issue #73).
3。 ActionDispatch::Executor
机架中间件
最后,您可以使用 Rails 现在用于 ActiveRecord 连接管理的相同代码,方法是将 ActionDispatch::Executor
添加到您的 Rack 中间件堆栈,并调用 ActiveRecord::QueryCache#install_executor_hooks
插入使用的钩子清除 ActiveRecord 连接:
require 'action_dispatch/middleware/executor'
use ActionDispatch::Executor, ActiveSupport::Executor
ActiveRecord::QueryCache.install_executor_hooks
我们正在将 Sinatra application from ActiveRecord 4 升级到 ActiveRecord 5。之前我们有这一行:
use ActiveRecord::ConnectionAdapters::ConnectionManagement
这是因为请求完成后连接没有被清理。这是关于此主题的先前 SO 讨论:
- ActiveRecord connection warning. (Database connections will not be closed automatically)
- ActiveRecord::ConnectionTimeoutError
从 ActiveRecord 5 开始,此行不再有效。这个conversation in the rails project states:
This was removed in favor of Executor and Reloader APIs. That middleware that was removed were not part of the public API. If you want to use that outside Rails you need to make one.
这是否意味着,如果有人要将 ActiveRecord 5 与 Sinatra 一起使用,连接将再次 'leaked' 或在请求后不返回到池中,除非开发人员重新创建现在-删除中间件?
在 Sinatra 示例中,我们现在是否需要在 ActiveRecord 5 中包含这一行?
after do
ActiveRecord::Base.clear_active_connections!
end
这就是链接线程的含义,但我想得到一个明确的答案,我可以带回我的开发团队。
你是对的,ConnectionManagement
中间件已从 ActiveRecord 5 (PR #23807) 中删除,因此在 Rails 之外设置 ActiveRecord 时需要复制类似的功能.有几种方法可以做到这一点:
1。 ConnectionManagement
机架中间件
ConnectionManagement
class不是很复杂。您可以将实现复制并粘贴到本地应用程序的某处,并像往常一样将其包含到您的 Rack 中间件堆栈中:
class ConnectionManagement
def initialize(app)
@app = app
end
def call(env)
testing = env['rack.test']
status, headers, body = @app.call(env)
proxy = ::Rack::BodyProxy.new(body) do
ActiveRecord::Base.clear_active_connections! unless testing
end
[status, headers, proxy]
rescue Exception
ActiveRecord::Base.clear_active_connections! unless testing
raise
end
end
use ConnectionManagement
2。 (特定于 Sinatra)连接管理 after
挂钩
在 Sinatra 应用程序中,您建议的块应该有效:
after do
ActiveRecord::Base.clear_active_connections!
end
注意这也是方法currently used by the sinatra-activerecord
integration gem to support ActiveRecord 5 (see issue #73).
3。 ActionDispatch::Executor
机架中间件
最后,您可以使用 Rails 现在用于 ActiveRecord 连接管理的相同代码,方法是将 ActionDispatch::Executor
添加到您的 Rack 中间件堆栈,并调用 ActiveRecord::QueryCache#install_executor_hooks
插入使用的钩子清除 ActiveRecord 连接:
require 'action_dispatch/middleware/executor'
use ActionDispatch::Executor, ActiveSupport::Executor
ActiveRecord::QueryCache.install_executor_hooks