使用断开连接的 Sequel::Database 对象进行查询时应该发生什么?

What should happend when using a disconnected Sequel::Database object for querying?

我现在面临的情况是这样的:

  1. 初始化数据库连接:
DB = Sequel.connect('<my-connection-string>')
  1. 像往常一样将其用于原始查询(在本例中为SQL):
DB['SELECT * FROM my-db'].each do |row|
    puts 'It works (as it should)'
end
  1. 从该数据库的池中断开所有连接:
DB.disconnect
  1. 再次使用断开连接的数据库对象进行查询(无需再次显式连接):
DB['SELECT * FROM my-db'].each do |row|
    puts 'It STILL works (I did not expect it)'
end

正如我在我的连接检查器中观察到的那样,即使在断开连接后,重新使用该对象最终也会打开一个新连接,指向该对象最初配置为连接的同一个数据库,并且一切正常。

我最终在 class 的 initialize 时间配置了一个简单的连接,但在查询循环的每次迭代后关闭它,令我惊讶的是它运行良好,没有任何抱怨.

我的问题是:这是它应有的行为方式吗?这是记录在案的行为还是只是未定义的行为,由于Sequel方面的良好设计模式(我猜?),最终行为一致?到目前为止,我还没有找到任何东西,无论是在 Sequel's documentation 还是在 SO.

我想继续使用这种格式(因为在我的特定情况下它节省了很多代码行),但前提是它是可靠的。

这是贡献者的预期行为 Jeremy Evans

It is expected behavior. Database#disconnect removes connections from the connection pool. When a new connection is needed, since the connection pool is empty, Sequel creates a new connection at that time. I think this would only seem like a bug to you if you are thinking that a Database object represents a single connection, when it does not.