[JAVA-849] No Such Method Exception When Instantiating Mongo Client Created: 07/Jun/13  Updated: 19/Oct/16  Resolved: 07/Jun/13

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Mike Delano Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

When creating a new mongo client:

MongoClient mongo = new MongoClient( "localhost");

I'm getting an exception:

java.lang.NoSuchMethodError: com.mongodb.ReadPreference.primary()Lcom/mongodb/ReadPreference;
	at com.mongodb.MongoClientOptions$Builder.<init>(MongoClientOptions.java:51)
	at com.mongodb.MongoClient.<init>(MongoClient.java:126)
	at com.mongodb.MongoClient.<init>(MongoClient.java:91)
	at com.mediasilo.commons.data.database.MongoDB.getDatabase(MongoDB.java:24)
	at test.com.mediasilo.commons.data.dao.MongoDBTests.getDatabase_suceeds(MongoDBTests.java:12)

I have recreated this issue on the following version of the java driver:

2.11.1
2.9.3

OS: Ubuntu 12.04 LTS
Java Version: 1.7.0_17

My code is as follows:

 
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
 
...
 
    public static final DB getDatabase() {
        DB db = null;
        try {
            MongoClient mongo = new MongoClient( "localhost");
            db = mongo.getDB(databaseName);
        }
        catch(UnknownHostException e) {
            log.error(ExceptionUtils.getStackTrace(e));
            Gmailer.send(e);
        }
        return db;
    }



 Comments   
Comment by Jeffrey Yemin [ 07/Jun/13 ]

No problem.

Comment by Mike Delano [ 07/Jun/13 ]

I found the issue. You were right, we were using Kundera which had an older version dependency. I've updated to 2.11 and it's resolved.

Thanks Jeff.

Comment by Jeffrey Yemin [ 07/Jun/13 ]

I bet you have an older version of the driver in your classpath. Try running with -verbose:class to see where the ReadPreference class is being loaded from.

Generated at Thu Feb 08 08:53:13 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.