4

我正在尝试使用 Java 的 Mongodb 驱动程序,我对这个偶尔出现的错误感到非常沮丧,尽管最一致的是,它出现在我第一次尝试连接到数据库时。任何关于在哪里寻找的帮助都会非常有帮助。

设置基本上是这样的:我在 localhost 上启动我的 Play 2.0 应用程序,我尝试在我的应用程序上注册一个用户。当我输入用户名时,会向数据库发送一个查询以检查用户名是否存在,这需要一秒钟(可能是实例化 Mongo 单例和连接到数据库的开销)但似乎有效,我可以成功执行几个查询,例如这个:

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;
}

然后当我尝试插入用户时出现问题(总共大约 2kb 的数据):

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;

这偶尔会失败,但往往if(result.getLastError().ok())在线上失败。我真的无法解决这个问题,因为它会随机弹出。以下是堆栈跟踪通常的样子:

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]

任何帮助将不胜感激,因为我无法真正弄清楚这里发生了什么,更有经验的人可能知道去哪里找。

一个可能也说明这一点的注意事项是,我尝试将更多数据加载到保存的对象中时,问题发生的频率越高。

谢谢!

4

0 回答 0