connection.database

31 views
Skip to first unread message

Ugo Bourdon

unread,
Aug 18, 2016, 5:23:07 AM8/18/16
to ReactiveMongo - http://reactivemongo.org
Hi,

I am looking for advice for use connection.database.

When i configure my mongo connection for all my app i use scala object reference lazy val that call connection.database.

It is a good pratice to do that or that can produce somme errors ? It is better to use def instead of lazy val to call connection.database ?

Object Connection {
  lazy val connection: MongoConnection
= //get my mongo connection
 
  lazy val db: Future[DefaultDB] = connection.database
  // or
  def db: Future[DefaultDB] = connection.database
}


In fact I use :
reactiveMongoApi.database

It change something about that ?

Thx for your response.

Cédric Chantepie

unread,
Aug 18, 2016, 5:38:25 AM8/18/16
to ReactiveMongo - http://reactivemongo.org
Using a val, even lazy, for the database references is not a good practice. That leads to having a previously badly resolved reference that will never be recovered (e.g. when the MongoDB RS is back on line). The good practice is to use def, considering that this references are lightweight (contrary to the MongoConnection representing the connection pool).
Reply all
Reply to author
Forward
0 new messages