我是否需要在 Ruby 和 Sinatra 中释放/关闭合用的 mongodb 连接?我看了http://api.mongodb.org/ruby/current/#Connection_Pooling但没有看到任何具体内容。
但是,我偶尔会在流量相对较低的应用程序中遇到此错误:
Error in the smoke test block could not obtain connection within 10 seconds. The max pool size is currently 10; consider increasing the pool size or timeout.: ["/var/lib/gems/1.9.1/gems/mongo-1.9.1/lib/mongo/util/pool.rb:274:in `block in checkout'",
我目前使用配置 block 来设置连接:
configure do
#Reading the file
ini = IniFile.new(:filename=>file_name,:parameter => '=')
section = ini['server']
env = section['environment']
mongoip = section['mongo_host']
mongo_port = section['mongo_port']
$environment = section['environment']
begin
$mongo_client = MongoClient.new(mongoip, mongo_port, :pool_size => 10, :pool_timeout => 10,:connect_timeout =>10)
rescue Exception => e
"Error connecting to the database"
end
end
我在这里错过了什么?
最佳答案
future 的注意事项:虽然这在当时可能是正确的,但 Moped 不再积极开发,前 Moped 开发人员和 MongoClient 开发人员都是 now working actively在 MongoClient 上。
我强烈建议使用 Moped而不是 MongoClient(它真的很旧并且有很多依赖项)。
Moped 自动处理连接池、超时、重新连接并且很智能。您可以在 https://github.com/mongoid/moped/blob/master/lib/moped/connection/pool.rb 找到有关连接池的详细信息。
Moped 是一个独立的 gem,没有依赖项,是 MongoDB 的纯 Ruby 驱动程序。 http://mongoid.org/en/moped/
当你这样做的时候,使用 Origin另一个独立的 gem,它为 MongoDB 查询公开了一个优秀的 DSL。
关于ruby - 使用 Sinatra 和 MongoDB- 关闭连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17976332/