[2.1] Play 2.1 (Java) + MongoDB without module? or which module?

607 views
Skip to first unread message

sub...@hotmail.com

unread,
May 22, 2013, 3:01:56 AM5/22/13
to play-fr...@googlegroups.com
Hi, guys.
 
Is it possible using MongoDB without any modules?
I've been trying several modules(Morphia, Jackson.. and I think ReactiveMongo is for Scala) googling all day,
but nothing seems right to use for Play 2.1 (Java). --> all tutorial's test failed.
 
Does anyone knows how to use MongoDB for Play 2.1 / Java language??
It's not easy. help.
 
 

Vikram

unread,
May 22, 2013, 1:13:23 PM5/22/13
to play-fr...@googlegroups.com
Hi 

I have been using jongo (http://jongo.org/) with Play Java, it is easy to use and performs well. Add the following dependencies to your Build.scala -

  val appDependencies = Seq(

    // Add your project dependencies here,

   "org.mongodb" % "mongo-java-driver" % "2.10.1",

   "org.jongo" % "jongo" % "0.4"

  )

Vikram 

shrike71

unread,
Dec 18, 2013, 7:53:22 AM12/18/13
to play-fr...@googlegroups.com
Hi,

Been trying to find out how to do this in the Global object to no avail. I can create mongodb connections as i require in Java, but the mongodb website recommends that a single instance of the connection is used due to the connection pooling. In Java Play (2.2.x) how on earth do I create a static MongoClient (or accessor) in the Global object that I can easily access from within my model or service objects?? I can't seem to reference the Global object anywhere from my models/services/controllers. I'm more than reasonably certain it can be done, it's just that i haven't found any documentation on how to do it yet!

This is a sample of what i've tried:

public class Global extends GlobalSettings {
   
    protected static CityDataService cs = new CityDataService();
   
    public static MongoClient mongoClient;
   
    public void onStart(Application app) {
       
        try {
            mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost"));
        } catch (UnknownHostException e) {
            System.err.println("Connection failed: " + e);
        }
        //cs.setRateCache();
        cs.setRouteCache();
        Logger.info("Application has started");
    }

    public void onStop(Application app) {
        cs.clearCache();
        mongoClient.close();
        Logger.info("Application shutdown...");
    }
   
    public MongoClient getMConnection(){
        return mongoClient;
    }
}



P




On Wednesday, 22 May 2013 10:32:24 UTC+1, vblagovic wrote:
Sure, take look at http://mongojack.org

I'm using it with plain mongo-java-driver.
Extend GlobalSettings, setup your connection with driver, reference it later on in your collection ( mongojack ).

vblagovic

unread,
Dec 18, 2013, 8:36:29 AM12/18/13
to play-fr...@googlegroups.com
Hi shrike71,
You should be able to access Global.mongoClient since it is public, static.
If you are looking for method getMConnection than you can simple put it also as static.
And you do have single instance. onStart is called only one time.

Roshan Kumar

unread,
Jun 3, 2016, 7:28:58 AM6/3/16
to play-framework

If you want to ODM that use java async driver you can use it

https://github.com/raimdtu/PlayAsyncJavaMongoODM

It will give you overview so you can implement your own ODM .

Reply all
Reply to author
Forward
0 new messages