1

I am using mongo with play framework with "reactivemongo", That makes an async bridge between mongo connection and programm. For standalone projects I always use casbah lib - it has more native syntax (sometimes using of Futures in each request is not needed, and my religion does not allow me to use Async.await for blocking each request ) for me and no actors overhead, also I don't like JSON BSON conversion overhead.

But using casbah in play framework direct way(just create Mongo connection in controller ) produces connection leaks - this means you should create connection pool and control yourself, otherwords write reactivemongo.

Has anybody used casbah with mongo in production ? Where is the best and most canonical way of creating and controlling connection in play ecosystem ?

Oleg
  • 3,080
  • 3
  • 40
  • 51

3 Answers3

2

First you should check Connecting to MongoDB. Now go to this tutorial create scala project ( If you used other editor then follow scala project creation steps ).

Now check this following steps :

1> projectName/conf/application.conf add application.conf mongo Db name, collection name, port number, URL etc. play reactive mongo for ex: I added following in my application.conf

mongodb.default.host = "localhost"
mongodb.default.db = "Demo"
mongodb.default.port = "27017"
CI.default.uri = "mongodb://localhost:27017/"

2> Create a .scala file in controller folder give any name for ex. I set file name as ScalaMongoFactory and add following code in this file

import com.mongodb.casbah. {
  MongoClient, MongoClientURI
}
import com.typesafe.config.ConfigFactory
object ScalaMongoFactory {
  private val config = ConfigFactory.load()
  private val DATABASE = config.getString("mongodb.default.db")
  private val server = MongoClientURI(config.getString("CI.default.uri"))
  private val client = MongoClient(server)
  val database = client(DATABASE)
}

3> Now create a new .scala file in controller where you want to use mongo connection. For ex. I created checkConnection.scala file and contains like

import com.cloudinsights.scala.controllers. {
  ScalaMongoFactory
}
object checkConnection {
  val collection = ScalaMongoFactory.database("your collectionName")
}
Neo-coder
  • 7,715
  • 4
  • 33
  • 52
0

There's no need to use Async.wait with reactive mongo (and you shouldn't anyway).

Juha
  • 1
  • 1
    Could you work a bit more on your answer? For a good answer, this is too short, not enough informative. E.g. **what** do you suggest instead of Async.wait? **Why** there's no need? – ZygD Apr 26 '15 at 10:05
0

I guess you could use a utility object to manage your connection.

import com.mongodb.casbah.{MongoClient, MongoDB}
import play.api.Play

object MongoManager {
    private val server = Play.current.configuration.getString("db.host").get
    private val port = Play.current.configuration.getInt("db.port").get

    object using {
        def apply[A](col: String)(block: MongoDB => A): A = {
            val con = MongoClient(server, port)
            val a = block(con.apply(col))
            con.close
            a
        }
        def apply[A](block: MongoClient => A): A = {
            val con = MongoClient(server, port)
            val a = block(con)
            con.close
            a
        }
    }

    object stashed {
        private lazy val con = MongoClient(server, port)
        def apply(col: String): MongoDB = con.apply(col)
        def apply: MongoClient = con
    }
}

I didn't find a play plugin for this driver.

Personally I'd recommend using the ReactiveMongo driver instead, since it can also use play's JSON library. If you're taking data from the database and feeding it through a REST api, its a nicer option.