续集 西纳特拉 + Phusion 乘客 + MySQL 连接管理

发布于 2024-07-26 19:21:44 字数 168 浏览 2 评论 0原文

我们使用 Sinatra 和 Sequel 来实现小型 API。 然而,我们遇到的问题是,在每个页面请求上,Sequel 都会打开与 MySQL 的新连接,并保持它们打开状态,直到超时,或者重新启动 Apache。

关于如何重用连接的文档并不多,因此任何帮助、解释和/或正确方向的指示都会有所帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可爱咩 2024-08-02 19:21:44

我将 Sequel 的内容包装在一个小包装器中并重用该包装器,如下所示:

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

或者,您可以在完成后调用 @con.disconnect 或使用块调用 Sequel.connect:

Sequel.connect("...") do |c|
   # work with c
end #connection closed 

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 
反目相谮 2024-08-02 19:21:44

我们发现我们做错了什么。 这是相当愚蠢的,我们在 Sinatra 的 before 过滤器中初始化了 Sequel。

因此,我们这样做:

DB = Sequel.mysql("...")

然后我们只需使用 DB 常量来使用 Sequel。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文