scala - play framework with mongodb -
i using mongo play framework "reactivemongo", makes async bridge between mongo connection , programm. standalone projects use casbah lib - has more native syntax (sometimes using of futures in each request not needed, , religion not allow me use async.await blocking each request ) me , no actors overhead, don't json bson conversion overhead.
but using casbah in play framework direct way(just create mongo connection in controller ) produces connection leaks - means should create connection pool , control yourself, otherwords write reactivemongo.
has used casbah mongo in production ? best , canonical way of creating , controlling connection in play ecosystem ?
first should check connecting mongodb. go tutorial create scala project ( if used other editor follow scala project creation steps ).
now check following steps :
1> projectname/conf/application.conf
add application.conf mongo db name, collection name, port number, url etc. play reactive mongo ex: added following in application.conf
mongodb.default.host = "localhost" mongodb.default.db = "demo" mongodb.default.port = "27017" ci.default.uri = "mongodb://localhost:27017/"
2> create .scala file in controller
folder give name ex. set file name scalamongofactory
, add following code in 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> create new .scala file in controller want use mongo connection. ex. created checkconnection.scala file , contains
import com.cloudinsights.scala.controllers. { scalamongofactory } object checkconnection { val collection = scalamongofactory.database("your collectionname") }
Comments
Post a Comment