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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值