我们将Play Framework 2.4与mongodb 2.6一起使用。但是,由于我们将反应性从0.11.10
迁移到0.11.11-play24
,因此出现了问题。
我们在应用程序启动期间将一些数据加载到mongodb中。一切都很好,应用程序启动,日志中没有发生错误但没有数据插入数据库。
以下是Global.scala
中应用程序启动的代码:
override def onStart(app: Application) {
Logger.info("creating object in database...")
someProducts.foreach{ product =>
ProductDB.insertOrUpdate(product._id, product)
}
}
为了注入reactivemongo,我们暂时决定不使用@Inject()
,因此我们改为使用current.injector
(根据Reactivemongo documentation)。
注意:我们完全删除了ReactiveMongoPlugin的所有痕迹,因为它已在play-reactivemongo 0.11.11-play24
中弃用。
在这里我们定义我们的模型并注入ReactiveMongo:
abstract class MongoDB[T: Format, ID: Format] {
def collection: JSONCollection
def insertOrUpdate( _id: ID, o: T) = collection.update(Json.obj("_id" -> _id), o, upsert = true)
}
object ProductDB extends MongoDB[Product, String] {
override def collection = current.injector.instanceOf[ReactiveMongoApi].db.collection("products")
}
在application.conf
中,使用此行启用了ReactiveMongoModule:
play.modules.enabled += "play.modules.reactivemongo.ReactiveMongoModule"
应用程序的日志:
[debug] p.a.l.c.ActorSystemProvider - Starting application default Akka system: application
[info] application - starting application, build at: 2016-05-17T13:00:18+0200
[info] application - creating object in database...
[info] application - ReactiveMongoApi starting...
[info] application - ReactiveMongoApi successfully configured with DB 'application'! Servers:
[localhost:27017]
[info] play.api.Play - Application started (Dev)
这是来自mongodb的日志:
2016-05-17T13:00:27.774+0200 [initandlisten] connection accepted from 127.0.0.1:65478 #3 (1 connection now open)
2016-05-17T13:00:27.833+0200 [conn3] run command admin.$cmd { ismaster: 1 }
2016-05-17T13:00:27.833+0200 [conn3] command admin.$cmd command: isMaster { ismaster: 1 } ntoreturn:1 keyUpdates:0 numYields:0 reslen:178 0ms
2016-05-17T13:00:27.880+0200 [initandlisten] connection accepted from 127.0.0.1:65479 #4 (2 connections now open)
2016-05-17T13:00:27.880+0200 [initandlisten] connection accepted from 127.0.0.1:65480 #5 (3 connections now open)
2016-05-17T13:00:27.880+0200 [initandlisten] connection accepted from 127.0.0.1:65481 #6 (4 connections now open)
2016-05-17T13:00:27.880+0200 [initandlisten] connection accepted from 127.0.0.1:65482 #7 (5 connections now open)
2016-05-17T13:00:27.880+0200 [initandlisten] connection accepted from 127.0.0.1:65483 #8 (6 connections now open)
2016-05-17T13:00:27.881+0200 [initandlisten] connection accepted from 127.0.0.1:65484 #9 (7 connections now open)
2016-05-17T13:00:27.881+0200 [initandlisten] connection accepted from 127.0.0.1:65485 #10 (8 connections now open)
2016-05-17T13:00:27.881+0200 [initandlisten] connection accepted from 127.0.0.1:65486 #11 (9 connections now open)
2016-05-17T13:00:27.882+0200 [initandlisten] connection accepted from 127.0.0.1:65487 #12 (10 connections now open)
2016-05-17T13:00:27.888+0200 [conn3] run command admin.$cmd { ismaster: 1 }
2016-05-17T13:00:27.888+0200 [conn3] command admin.$cmd command: isMaster { ismaster: 1 } ntoreturn:1 keyUpdates:0 numYields:0 reslen:178 0ms
2016-05-17T13:00:27.888+0200 [conn3] run command admin.$cmd { ismaster: 1 }
2016-05-17T13:00:27.889+0200 [conn3] command admin.$cmd command: isMaster { ismaster: 1 } ntoreturn:1 keyUpdates:0 numYields:0 reslen:178 0ms
2016-05-17T13:00:27.889+0200 [conn3] run command admin.$cmd { ismaster: 1 }
2016-05-17T13:00:27.889+0200 [conn3] command admin.$cmd command: isMaster { ismaster: 1 } ntoreturn:1 keyUpdates:0 numYields:0 reslen:178 0ms
2016-05-17T13:00:27.889+0200 [conn3] run command admin.$cmd { ismaster: 1 }
2016-05-17T13:00:27.889+0200 [conn3] command admin.$cmd command: isMaster { ismaster: 1 } ntoreturn:1 keyUpdates:0 numYields:0 reslen:178 0ms
2016-05-17T13:00:27.889+0200 [conn3] run command admin.$cmd { ismaster: 1 }
2016-05-17T13:00:27.889+0200 [conn3] command admin.$cmd command: isMaster { ismaster: 1 } ntoreturn:1 keyUpdates:0 numYields:0 reslen:178 0ms
2016-05-17T13:00:27.890+0200 [conn3] run command admin.$cmd { ismaster: 1 }
2016-05-17T13:00:27.890+0200 [conn3] command admin.$cmd command: isMaster { ismaster: 1 } ntoreturn:1 keyUpdates:0 numYields:0 reslen:178 0ms
2016-05-17T13:00:27.890+0200 [conn3] run command admin.$cmd { ismaster: 1 }
2016-05-17T13:00:27.890+0200 [conn3] command admin.$cmd command: isMaster { ismaster: 1 } ntoreturn:1 keyUpdates:0 numYields:0 reslen:178 0ms
2016-05-17T13:00:27.891+0200 [conn3] run command admin.$cmd { ismaster: 1 }
2016-05-17T13:00:27.891+0200 [conn3] command admin.$cmd command: isMaster { ismaster: 1 } ntoreturn:1 keyUpdates:0 numYields:0 reslen:178 0ms
2016-05-17T13:00:27.891+0200 [conn3] run command admin.$cmd { ismaster: 1 }
2016-05-17T13:00:27.891+0200 [conn3] command admin.$cmd command: isMaster { ismaster: 1 } ntoreturn:1 keyUpdates:0 numYields:0 reslen:178 0ms
答案 0 :(得分:0)
您需要在加载数据库后插入:
def db: DefaultDB = Await.result(reactiveMongoApi.database, Duration.Inf)
现在,reactiveMongoApi.db
是同步的,如果直接使用,连接尚未完成(生命周期)。
所以我建议你要么等待数据库(如上面的代码),要么你只是将回调注册到一个缓冲区,这将在数据库初始化时被执行
reactiveMongoApi.database.onSuccess {
case _ => callbacks.foreach(_())
}