0

I have the following singleton object which has a static method called connect which returns a DB connection. In classical synchronous programming I am understood to believe that you only ever want one instance of a connection however this seems at odds with the asynchronous model of the reactiveMongo driver which uses an underlying the Actor (Akka) model.

   object MyMongoDriver  {



    def connect(uri: String) {



      val driver = new MongoDriver 
      val connection: Try[MongoConnection] =  
        MongoConnection.parseURI(uri).map {
          parsedURI => driver.connection(parsedURI) 
      }



  }
}

What seems to be happening to me though is that one instance of MyMongoDriver is instantiated, and then multiple (as many as needed) connections are returned each time connect is called? I don't think I just introduced blocking, or have I? The rest of the asynchronous behavior I suspect continues to happen by design given reactivemongo is reactive. Is there a better way to handle connections?

Faktor 10
  • 1,868
  • 2
  • 21
  • 29
  • See [updated documentation](http://reactivemongo.org/releases/0.11/documentation/tutorial/play2.html) with `example` using `injector`. – cchantep Jul 15 '15 at 19:04

2 Answers2

1

As indicated in the documentation "A MongoDriver instance manages an actor system; A connection manages a pool of connections. In general, a MongoDriver or a MongoConnection is never instantiated more than once."

cchantep
  • 9,118
  • 3
  • 30
  • 41
  • Thank you for that, and I have already read the documentation. My question specifically I suppose is should I apply the singleton pattern or is this taken care of for me, it wasn't clear in teh documentation. My hunch is that it is taken care of and I can just use the driver without my own singleton boilerplate? – Faktor 10 Jul 09 '15 at 09:47
-1

You have to use @Singleton to make a Mongodb driver singleton.

Please read this UserDAOMongo as an example, https://github.com/luongbalinh/play-mongo/blob/master/app/dao/mongo/impl/UserDAOMongo.scala

Luong Ba Linh
  • 802
  • 5
  • 20