与 Sinatra 应用程序中包含的类共享数据库连接

发布于 2024-08-28 13:59:05 字数 568 浏览 4 评论 0原文

我正在将 Rails 应用程序的一部分转换为其自己的 sinatra 应用程序。它有一些重要的工作要做,而不是在 app.rb 中提供一百万个帮助,我将其中一些分成了类。如果无法访问 Rails,我将重写 finder 的几种方法,并需要访问类中的数据库。在应用程序和类之间共享数据库连接的最佳方式是什么?或者您是否建议将所有数据库工作推入其自己的类中,并且仅在那里建立连接?

这是我在 app.rb 中的内容,

require 'lib/myclass'

configure :production do
  MysqlDB = Sequel.connect('mysql://user:password@host:port/db_name')
end

我想在 lib/myclass.rb 中访问它,

class Myclass
  def self.find_by_domain_and_stub(domain, stub)
    # want to do a query here
  end
end

我已经尝试了几件事,但似乎没有什么效果足以包含作为示例。

I'm converting a part of a rails application to its own sinatra application. It has some beefy work to do and rather than have a million helps in app.rb, I've separated some of it out into classes. Without access to rails I'm rewriting finder several methods and needing access to the database inside of my class. What's the best way to share a database connection between your application and a class? Or would you recommend pushing all database work into its own class and only having the connection established there?

Here is what I have in in app.rb

require 'lib/myclass'

configure :production do
  MysqlDB = Sequel.connect('mysql://user:password@host:port/db_name')
end

I want to access it in lib/myclass.rb

class Myclass
  def self.find_by_domain_and_stub(domain, stub)
    # want to do a query here
  end
end

I've tried several things but nothing that seems to work well enough to even include as an example.

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

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

发布评论

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

评论(2

初懵 2024-09-04 13:59:06

蒂姆·罗森布拉特 (Tim Rosenblatt) 的回答将根据每个请求重新连接。最好在您的 Sinatra 应用程序中执行以下操作:

require 'sequel'
DB = Sequel.connect('mysql://user:password@host:port/db_name')
require 'lib/myclass'

在这种情况下,最好使用常量而不是全局变量。

Tim Rosenblatt's answer will reconnect on every request. It's better to do the following in your Sinatra app:

require 'sequel'
DB = Sequel.connect('mysql://user:password@host:port/db_name')
require 'lib/myclass'

It's better to use a constant than a global variable in this case.

初见你 2024-09-04 13:59:05

假设您没有进行任何线程处理,只需将连接设置为全局变量即可。

require 'lib/myclass'

before do
  $MysqlDB = Sequel.connect('mysql://user:password@host:port/db_name')
end

class Myclass
  def self.find_by_domain_and_stub(domain, stub)
    # use $MysqlDB here
  end
end

检查连接超时设置或明确与服务器断开连接可能是明智之举。如果您正在处理大量请求,则可能会在超时之前耗尽连接。

Assuming you're not doing any threading, just set up the connection as a global var.

require 'lib/myclass'

before do
  $MysqlDB = Sequel.connect('mysql://user:password@host:port/db_name')
end

class Myclass
  def self.find_by_domain_and_stub(domain, stub)
    # use $MysqlDB here
  end
end

Might be wise to check the connection timeout settings, or explicitly disconnect from the server. If you're handling a lot of requests, you could run out of connections before they time out.

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