What happens when connections to MongoDB are not closed?

大憨熊 提交于 2019-12-04 07:25:12

You should not be calling ->close() on every iteration. If you call close, you tell the driver to not reuse a persistent connection. If you run this in a tight loop, then the OS runs out of ports to use, as they are all in TIME_WAIT state.

The PHP driver uses persistent connections, and if (without calling ->close) you run "new Mongo" in a tight loop like in your example, the driver will not make new connections and reuse the already existing one.

You would get a fatal Error:

PHP Fatal error: Uncaught exception 'MongoConnectionException' with message 'Cannot assign requested address' in /home/pierre/test.php:3 Stack trace: #0 /root/test.php(3): Mongo->__construct('mongodb://local...') #1 {main} thrown in /home/pierre/test.php on line 3

c=0; while [ $c -le 20000 ]; do php test.php; c=$[c+1]; done

Testet with a mongodb and default Configuration. MongoDB keeps running and other scripts keep doing their job. Maybe you should consider using something like a jobserver, gearman for example.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!