为什么在重新编译后第一次访问数据库时会出现套接字超时?

时间:2012-06-13 14:46:00

标签: mongodb playframework-2.0 mongodb-java

我正在使用playframework 2.0,每次重新编译后,我的应用程序第一次尝试转到数据库时都会收到套接字超时。我直接使用Mongo Driver。这是典型的堆栈跟踪:

play.core.ActionInvoker$$anonfun$receive$1$$anon$1: Execution exception [[Network: can't call something : ds031907.mongolab.com/107.21.99.26:31907/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 : ds031907.mongolab.com/107.21.99.26:31907/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.DBCollection.findOne(DBCollection.java:647) ~[mongo-java-driver-2.7.3.jar:na]
at com.mongodb.DBCollection.findOne(DBCollection.java:626) ~[mongo-java-driver-2.7.3.jar:na]
at models.daos.ModuleDAO.findPublishedModuleById(ModuleDAO.java:445) ~[classes/:na]
at controllers.LearnController.viewModule(LearnController.java:31) ~[classes/:2.0]
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]

这是我的初始化代码:

public static DB getDB(){
    ensureMongo();
    DB db = mongo.getDB(MOJULO_DB);
    if(!db.isAuthenticated()){
        db.authenticate(MONGO_USERNAME, MONGO_PASSWORD);
        if(db.isAuthenticated())
            System.out.println("authentication success on db:" +  db.getName());
        else
            System.out.println("db authentication failure");
    }
    return db;
}
private static synchronized void ensureMongo(){
    if(mongo == null){
        try{
            MongoURI mongoURI = new MongoURI(MONGO_URI);
            mongo = new Mongo(mongoURI);
            DB db = mongo.getDB(MOJULO_DB);
            db.command("ping");
        }catch(UnknownHostException ex){
            mongo = null;
            System.out.println("failed to connect to mongo");
            ex.printStackTrace();
        }
    }
}
public static void disconnect(){
    System.out.println("disconnecting from mongo");
    if(mongo != null){
        mongo.close();
        mongo = null;
    }
}

我使用类外部的getDB方法来获取数据库。该方法用于创建mongo单例(如果它不存在)。我总是获得认证成功println,但是在第一次点击数据库时,我得到套接字超时异常。

在我的Global类中,关闭应用程序时关闭与数据库的连接。

@Override
public void onStop(Application app) {
    System.out.println("stop");
    Logger.info("Application shutdown...");
    DBManager.disconnect();
} 

任何想法?

1 个答案:

答案 0 :(得分:0)

我不是MongoDB的专家,但可以看到与其他数据库连接的相似性。 你的连接是如何配置的? 它看起来(对我来说)当你第一次尝试使用数据库连接(查找方法)时它可能正在尝试加载所有映射/数据库和表定义/其他所有内容。 在ensureMongo()方法中运行一个简单的数据库查询可能会更好,以允许系统重新初始化它所需的一切(您可能需要在此方法上设置更长的超时)。