日期:2014-05-16  浏览次数:20451 次

mongodb客户端错误集合
错误一:
调用代码:
String map = "function() { emit(this.offer_price, {count:1});}";

        String reduce = "function(key, values) {";
        reduce = reduce + "var total = 0;";
        reduce = reduce + "for(var i=0;i<values.length;i++){total += values[i].count;}";
        reduce = reduce + "return {count:total};}";

        String result = "resultCollection";
        MapReduceOutput mapReduceOutput = coll.mapReduce(map, reduce.toString(), result, null);
        DBCollection resultColl = mapReduceOutput.getOutputCollection();
        DBCursor cursor = resultColl.find();
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }

Exception in thread "main" com.mongodb.MongoException$Network: can't call something : /10.20.141.58:28018/test
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:227)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:305)
at com.mongodb.DB.command(DB.java:160)
at com.mongodb.DB.command(DB.java:183)
at com.mongodb.DB.command(DB.java:144)
at com.mongodb.DBCollection.mapReduce(DBCollection.java:1054)
at com.mongodb.DBCollection.mapReduce(DBCollection.java:1008)
at TestMongodb.main(TestMongodb.java:120)
Caused by: java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
at org.bson.io.Bits.readFully(Bits.java:35)
at org.bson.io.Bits.readFully(Bits.java:28)
at com.mongodb.Response.<init>(Response.java:39)
at com.mongodb.DBPort.go(DBPort.java:128)
at com.mongodb.DBPort.call(DBPort.java:79)
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:218)
... 7 more


解决方法:
options.socketTimeout = 2000;


错误2:
Out of semaphores to get db connection
查看源代码发现是连接池资源用尽:
 public DBPort get(){
        DBPort port = null;
        if ( ! _waitingSem.tryAcquire() )
            throw new SemaphoresOut();



但是,我明明是设置过了啊...
 MongoOptions options = m.getMongoOptions();
            options.autoConnectRetry = true;
            options.connectionsPerHost = 500;
            options.maxWaitTime = 5000;
            options.socketTimeout = 0;
            options.connectTimeout = 15000;
            options.threadsAllowedToBlockForConnectionMultiplier = 4;


再仔细看了下初始化顺序,发现semaphore是在mongodb初始化的时候设置的。所以需要这样设置:

MongoOptions options = new MongoOptions();
            options.autoConnectRetry = true;
            options.connectionsPerHost = 1000;
            options.maxWaitTime = 5000;
            options.socketTimeout = 0;
            options.connectTimeout = 15000;
            options.threadsAllowedToBlockForConnectionMultiplier = 5000;
            m = new Mongo(new ServerAddress("10.20.141.22", 28018), options);