我有一个使用ReactiveMongo的基于Scala的Play应用。在开发模式和热重载发生时,ReactiveMongo泄漏连接。例如,这是一个Mongo的日志文件片段;我在日志中添加了一个换行符,表示每次发生热重载(即我对应用程序进行了微不足道的更改并在浏览器中重新加载了页面)
2015-09-26T09:43:43.353-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58612 #1 (1 connection now open)
2015-09-26T09:43:43.725-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58613 #2 (2 connections now open)
2015-09-26T09:43:43.725-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58614 #3 (3 connections now open)
2015-09-26T09:43:43.725-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58615 #4 (4 connections now open)
2015-09-26T09:43:43.725-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58616 #5 (5 connections now open)
2015-09-26T09:43:43.726-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58617 #6 (6 connections now open)
2015-09-26T09:43:43.726-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58618 #7 (7 connections now open)
2015-09-26T09:43:43.726-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58619 #8 (8 connections now open)
2015-09-26T09:43:43.726-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58620 #9 (9 connections now open)
2015-09-26T09:43:43.726-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58621 #10 (10 connections now open)
2015-09-26T09:43:43.810-0700 I INDEX [conn1] build index on: sma.destination properties: { v: 1, unique: true, key: { id: 1 }, name: "idx_destination_id", ns: "sma.destination" }
2015-09-26T09:43:43.810-0700 I INDEX [conn1] building index using bulk method
2015-09-26T09:43:57.164-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58640 #11 (11 connections now open)
2015-09-26T09:43:57.169-0700 I NETWORK [conn11] end connection 127.0.0.1:58640 (10 connections now open)
2015-09-26T09:43:57.431-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58641 #12 (11 connections now open)
2015-09-26T09:43:57.435-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58642 #13 (12 connections now open)
2015-09-26T09:43:57.436-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58643 #14 (13 connections now open)
2015-09-26T09:43:57.436-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58644 #15 (14 connections now open)
2015-09-26T09:43:57.437-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58645 #16 (15 connections now open)
2015-09-26T09:43:57.437-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58646 #17 (16 connections now open)
2015-09-26T09:43:57.437-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58647 #18 (17 connections now open)
2015-09-26T09:43:57.437-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58648 #19 (18 connections now open)
2015-09-26T09:43:57.437-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58649 #20 (19 connections now open)
2015-09-26T09:43:57.437-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58650 #21 (20 connections now open)
2015-09-26T09:43:57.487-0700 I INDEX [conn14] build index on: sma.destination properties: { v: 1, unique: true, key: { id: 1 }, name: "idx_destination_id", ns: "sma.destination" }
2015-09-26T09:43:57.487-0700 I INDEX [conn14] building index using bulk method
2015-09-26T09:44:16.559-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58677 #22 (21 connections now open)
2015-09-26T09:44:16.560-0700 I NETWORK [conn22] end connection 127.0.0.1:58677 (20 connections now open)
2015-09-26T09:44:16.766-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58678 #23 (21 connections now open)
2015-09-26T09:44:16.770-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58679 #24 (22 connections now open)
2015-09-26T09:44:16.771-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58680 #25 (23 connections now open)
2015-09-26T09:44:16.771-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58681 #26 (24 connections now open)
2015-09-26T09:44:16.771-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58682 #27 (25 connections now open)
2015-09-26T09:44:16.772-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58683 #28 (26 connections now open)
2015-09-26T09:44:16.772-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58684 #29 (27 connections now open)
2015-09-26T09:44:16.772-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58685 #30 (28 connections now open)
2015-09-26T09:44:16.772-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58686 #31 (29 connections now open)
2015-09-26T09:44:16.772-0700 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58687 #32 (30 connections now open)
2015-09-26T09:44:16.791-0700 I INDEX [conn24] build index on: sma.destination properties: { v: 1, unique: true, key: { id: 1 }, name: "idx_destination_id", ns: "sma.destination" }
2015-09-26T09:44:16.792-0700 I INDEX [conn24] building index using bulk method
您可以看到ReactiveMongo每次发生热重载时都会创建一个包含10个连接的池;之前的10个连接没有得到清理。最终它达到最大连接数(目前约为200),然后mongo变得无法访问,直到我重新启动。
据推测,这不会发生在刺激中,但我首先发现它在开发中有点不安。当应用程序热重载时,有什么方法可以关闭这些连接吗?
答案 0 :(得分:0)
在稍微过时的教程(使用Play 2.6)后出现同样的问题
来自文档: http://reactivemongo.org/releases/0.12/documentation/tutorial/play.html
/*
* Get a JSONCollection (a Collection implementation that is designed to work
* with JsObject, Reads and Writes.)
* Note that the `collection` is not a `val`, but a `def`. We do _not_ store
* the collection reference to avoid potential problems in development with
* Play hot-reloading.
*/
def collection: Future[JSONCollection] = database.map(
_.collection[JSONCollection]("persons"))
就我而言,将val collection: JSonCollection
替换为上述格式解决了我的问题。
现在重新加载时,它会关闭并重新打开所有连接:
2017-08-15T15:32:50.991-0300 I - [conn2] end connection 127.0.0.1:58489 (10 connections now open)
2017-08-15T15:32:50.992-0300 I - [conn9] end connection 127.0.0.1:58496 (9 connections now open)
2017-08-15T15:32:50.992-0300 I - [conn4] end connection 127.0.0.1:58491 (8 connections now open)
2017-08-15T15:32:50.993-0300 I - [conn5] end connection 127.0.0.1:58492 (7 connections now open)
2017-08-15T15:32:50.994-0300 I - [conn6] end connection 127.0.0.1:58493 (6 connections now open)
and so on...