1

我正在从事喷雾、akka、scala、reactivemongo 项目,我有这个特点

trait PersistenceManager {

  val driver = new MongoDriver
  val connection = driver.connection(List("localhost"))

  def loan[A](collectionName: String)(f: BSONCollection => Future[A]): Future[A] = {
    val db = connection("flujo_caja_db")
    val collection = db.collection(collectionName)
    f(collection)
  }
}

我也有 Dao 的对象来使用该特征,如下所示:

object Dao extends PersistenceManager {

   def find = loan("users"){
     collection =>
          collection.find(BsonDocument())....
   }

}

在我的persistencemanager trait 中实例化这些数据库值是正确的吗?它真的很好用。

谢谢!

4

1 回答 1

5

我想你想要这样定义的东西,以防止MongoDriver创建多个连接池:

object PersistenceManager{
  val driver = new MongoDriver
  val connection = driver.connection(List("localhost"))
}


trait PersistenceManager {
  import PersistenceManager._

  def loan[A](collectionName: String)(f: BSONCollection => Future[A]): Future[A] = {
    val db = connection("flujo_caja_db")
    val collection = db.collection(collectionName)
    f(collection)
  }
}
于 2014-05-13T17:07:29.897 回答