Mongodb Java Driver fails to read data sporadically, socket timeout

1.6k Views Asked by At

I am trying to use the Mongodb driver for java and I have been getting very frustrated with this bug that appears very sporadically, though most consistently, it appears when I am first trying to connect to the database. Any help on where to look would be incredibly helpful.

The set up is basically this: I start my Play 2.0 app on localhost, I attempt to register a user on my app. As I type the username, a query is sent to the database to check if the username exists, this takes a second (presumably overhead for instantiating the Mongo singleton and connecting to the database) but seems to work, I can execute several queries successfully like this:

public static boolean usernameIsAvailable(String username){
    DBCollection users = DBManager.getDB("mojulo").getCollection("users");
    DBCursor cursor = users.find(new BasicDBObject("username", username));
    if(cursor.count() == 0)
        return true;
    return false;
}

Then problem arises when I try to insert the user (which is a total of about 2kb of data):

BasicDBObject new_user = new BasicDBObject();
new_user.put("username", username);
...
//connect to the database
DBCollection users = DBManager.getDB("mojulo").getCollection("users");

//check if the email is already registered
DBCursor cursor = users.find(new BasicDBObject("email", email));
if(cursor.count() != 0){
    emailIsRegistered = true;
}

//check if the username is available
if(!usernameIsAvailable(username)){
    usernameIsTaken = true;
}

//if the email or username are taken, return null
if(emailIsRegistered || usernameIsTaken){
    System.out.println("failed to create user:" + username);
    return null;
}
WriteResult result = users.insert(new_user);
if(result.getLastError().ok()){
    System.out.println("successfully created user:" + username);
    return userRandomKey;
}else return null;

This fails pretty sporadically, but tends to fail on the if(result.getLastError().ok()) line. I can't really get a beat on the problem because it will pop up randomly. Here is what the stack trace generally looks like:

play.core.ActionInvoker$$anonfun$receive$1$$anon$1: Execution exception [[Network: can't call something : ds033307.mongolab.com/107.20.129.238:33307/heroku_app4620908]]
at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:82) [play_2.9.1.jar:2.0]
at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:63) [play_2.9.1.jar:2.0]
at akka.actor.Actor$class.apply(Actor.scala:290) [akka-actor.jar:2.0]
at play.core.ActionInvoker.apply(Invoker.scala:61) [play_2.9.1.jar:2.0]
at akka.actor.ActorCell.invoke(ActorCell.scala:617) [akka-actor.jar:2.0]
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:179) [akka-actor.jar:2.0]

Caused by: com.mongodb.MongoException$Network: can't call something : ds033307.mongolab.com/107.20.129.238:33307/heroku_app4620908
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:227) ~[mongo-java-driver-2.7.3.jar:na]
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:305) ~[mongo-java-driver-2.7.3.jar:na]
at com.mongodb.DB.command(DB.java:160) ~[mongo-java-driver-2.7.3.jar:na]
at com.mongodb.DB.command(DB.java:183) ~[mongo-java-driver-2.7.3.jar:na]
at com.mongodb.DBCollection.getCount(DBCollection.java:864) ~[mongo-java-driver-2.7.3.jar:na]
at com.mongodb.DBCollection.getCount(DBCollection.java:835) ~[mongo-java-driver-2.7.3.jar:na]

Caused by: java.net.SocketException: Operation timed out
at java.net.SocketInputStream.socketRead0(Native Method) ~[na:1.6.0_31]
at java.net.SocketInputStream.read(SocketInputStream.java:129) ~[na:1.6.0_31]
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218) ~[na:1.6.0_31]
at java.io.BufferedInputStream.read1(BufferedInputStream.java:258) ~[na:1.6.0_31]
at java.io.BufferedInputStream.read(BufferedInputStream.java:317) ~[na:1.6.0_31]
at org.bson.io.Bits.readFully(Bits.java:35) ~[mongo-java-driver-2.7.3.jar:na]

Any help would be greatly appreciated, because I can't really figure out what is going on here, and someone more experienced would probably know where to look.

One note that might also shed light on this is that the problem has been happening with greater frequency the more data I try to load into the saved object.

Thanks!

0

There are 0 best solutions below