0

i am having mongoDB connections issue in java , this is my connection class

    public MongoDbUtil() {
    try {
       System.out.println("1");
    String host = "127.0.0.1"  ; 
    String dbName = "m_prod" ; 
    int port =27017 ; 
       System.out.println("2");
    Mongo m = new Mongo();
       System.out.println("3");
    ds = new Morphia().createDatastore(m,dbName);
       System.out.println("4");
    ds.ensureIndexes(); 
       System.out.println("5");
    ds.ensureCaps();
       System.out.println("1");
    } catch(Exception e) {
       System.out.println("catch");            
    }finally{
       System.out.println("finally");
       System.out.println(ds==null);
    } }

only 1 and 2 is printing, after that 'finally' is printing also 'ds' is null, there is no any exception happen ('catch' is not printing)

Mongo server is up and running and i can access from command prompt (Linux) , the Other interesting thing is, its working fine when i call this method by unit test function, but for all other cases above issue happen , what can be the reason ?

Thanks

4
  • 1
    Try if catch (Throwable e) (instead of catch(Exception e)) shows some errors. Commented Jul 29, 2013 at 12:36
  • 3
    Use e.printStackTrace(). Otherwise your catch block is useless for any error information. Commented Jul 29, 2013 at 12:38
  • @Kayaman : program is not coming to catch block even , its not printing 'catch' which prints inside catch block Commented Jul 29, 2013 at 12:47
  • @nutlike : you are correct when i put Throwable it prints a error , its telling that 'com.mongodb.Mongo: method <init>()V not found' at the line which creates 'm' object (Mongo m = new Mongo();) Commented Jul 29, 2013 at 13:03

2 Answers 2

3
  1. Mongo() is deprecated, you should use MongoClient() instead - see http://api.mongodb.org/java/2.11.0/com/mongodb/Mongo.html#Mongo()
  2. Still it should find the deprecated constructor. Can you include the imports of your file, please?
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, what is the Package of 'MongoClient' class exist, i am using mongo java driver 3.0.0 ?
Are you sure about 3.0.0? The latest version should be 2.11.2. MongoClient() is a wrapper around Mongo() with safer default settings. Mongo() is still available for backwards compatibility, but shouldn't be used any longer.
I am try to update the version, but having a problem with Morphia version, i am using morphia 0.91 which used mongo-java-driver:2.8.0 ,because of that i cant add mongo-java-driver:2.11.2 dependency to my pom.xml file
Update to the latest Morphia release: 0.101.0. 0.91 must be really old...
Thanks to all , issue was fix by updating Morphia to 0.11.0 and java driver to 2.11.2 , thanks again
1

If you're using the 3.0 driver, there's a driver-compat layer that will help you transition. You really should use the new API, though.

3 Comments

Related to this: Do you provide a SNAPSHOT / alpha / beta of the Java driver? And how does it work in combination with Morphia?
We're just starting the process of testing morphia against the 3.0 driver. We're down to 4 test failures 2 of which are caused by the same issue. I'm not sure about public snapshots but I'll ask.
If you add this repository to your pom, you can get the 3.0 snapshot builds oss.sonatype.org/content/repositories/snapshots with this dependency: <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.0.0-SNAPSHOT</version> </dependency>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.