MongoDB:什么是连接池和超时? [英] MongoDB: What is connection pooling and timeout?

查看:563
本文介绍了MongoDB:什么是连接池和超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用Connection.new(localhost,3000,:pool_size => 1)连接到MongoDB, :timeout => 5)



为什么我需要一个连接池,如果我只是在启动我的Rails应用程序,而不是每个请求开销?为什么单个进程需要多于1个连接?



超时的目的是什么?为什么我想要连接超时?难道不应该坚持通过Rails进程的生命?



这么困惑...



这个问题是针对Ruby和Mongo的,但我想它适用于其他语言/数据库。

解决方案

这里不需要使用连接池。当您使用Passenger时,只需确保每个实例通过捕获start_worker_process事件使用单独的连接。这在驱动程序README中有记录。



连接池对于某些多线程应用程序可能很有用。 pool_size是连接将处理的并发线程的最大数量,timeout是线程在抛出异常之前可以等待可用套接字的最大秒数。


So my Passenger spins up 5 instances of my Rails app

I connect to MongoDB using Connection.new("localhost", 3000, :pool_size => 1, :timeout => 5)

Why would I need a "pool of connections" if I only incur overhead when starting up my Rails app, not per request? Why would a single process need more than 1 connection?

And what is the purpose of the timeout? Why would I want the connection to timeout? Shouldn't it be persist through the Rails process' lifetime?

So confused...

This question is specific to Ruby and Mongo but I guess it applies to other languages/databases.

解决方案

You don't need to use connection pooling here. As you're using Passenger, just make sure that each instance uses a separate connection by catching the start_worker_process event. This is documented in the driver README.

Connection pooling can be useful for certain multi-threaded apps. pool_size is the max number of concurrent threads the connection will handle, and timeout is the maximum number of seconds a thread can wait for an available socket before an exception is thrown.

这篇关于MongoDB:什么是连接池和超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆