2

我正在使用带有 java 的 mongoDb。插入数据时出现以下错误。任何帮助,将不胜感激。谢谢

Jul 4, 2012 1:45:32 PM org.xsocket.connection.HandlerAdapter performOnData
WARNING: [6f829b013850ff7914137a5cceC291] closing connection. Error occured by performing onData of com.avaya.onex.hss.requesthandlers.ResponseHandler#18746603 java.lang.OutOfMemoryError: Java heap space
Jul 4, 2012 1:45:25 PM com.mongodb.DBPortPool gotError
WARNING: emptying DBPortPool to localhost:27017 b/c of error
java.net.SocketException: Connection reset by peer: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
    at org.bson.io.PoolOutputBuffer.pipe(PoolOutputBuffer.java:129)
    at com.mongodb.OutMessage.pipe(OutMessage.java:111)
    at com.mongodb.DBPort.go(DBPort.java:119)
    at com.mongodb.DBPort.go(DBPort.java:89)
    at com.mongodb.DBPort.say(DBPort.java:84)
    at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:153)
    at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:138)
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:261)
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:211)
    at com.mongodb.DBCollection.insert(DBCollection.java:57)
    at com.mongodb.DBCollection.insert(DBCollection.java:102)
    at com.avaya.onex.hss.requesthandlers.DatabaseConnection.dbWrite(DatabaseConnection.java:50)
    at com.avaya.onex.hss.requesthandlers.ResponseHandler.handleResponse(ResponseHandler.java:232)
    at com.avaya.onex.hss.requesthandlers.ResponseHandler.onData(ResponseHandler.java:104)
    at org.xsocket.connection.HandlerAdapter.performOnData(HandlerAdapter.java:242)
    at org.xsocket.connection.HandlerAdapter.access$200(HandlerAdapter.java:42)
    at org.xsocket.connection.HandlerAdapter$PerformOnDataTask.run(HandlerAdapter.java:210)
    at org.xsocket.SerializedTaskQueue.performPendingTasks(SerializedTaskQueue.java:161)
    at org.xsocket.SerializedTaskQueue.access$100(SerializedTaskQueue.java:40)
    at org.xsocket.SerializedTaskQueue$MultithreadedTaskProcessor.run(SerializedTaskQueue.java:189)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

这是我有 dbWrite 函数的类,我从其他类调用该函数用于在 mongodb 中插入数据。

package com.avaya.onex.hss.requesthandlers;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoOptions;
import com.mongodb.WriteConcern;

public class DatabaseConnection {
    private static DatabaseConnection con = null;
    static DB db=null;
    static Mongo m;
    //private static int count=0;

    private DatabaseConnection(){
    }

    public void dbConnect() {

        try{
        // connect to the local database server
        MongoOptions options = new MongoOptions();
        options.connectionsPerHost = 9000000;
        options.maxWaitTime = 1000000;
        options.socketKeepAlive = true;
        options.threadsAllowedToBlockForConnectionMultiplier = 1000;

         m = new Mongo("localhost", options);

         //m.dropDatabase("ClientSimulator");
         db = m.getDB( "ClientSimulator" );

        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static synchronized DatabaseConnection getInstanceClass(){
        if(con==null)
            con=new DatabaseConnection();
        return con;
    }

    public DB getDatabaseObject(){
        return db;
    }

    public synchronized void dbWrite(DBCollection coll, BasicDBObject obj){
        coll.insert(obj);
        try {
            Thread.currentThread().sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public synchronized void dbRead(DBCollection coll){
    }

    public synchronized void dbUpdate(DBCollection coll) {
    }

    public synchronized void dbCollDrop(DBCollection coll){
        coll.drop();
    }
}
4

2 回答 2

4

尝试这个。

    options.connectionsPerHost = 100;
    options.maxWaitTime = 2000;
    options.socketKeepAlive = true;
    options.threadsAllowedToBlockForConnectionMultiplier = 50;

connectionsPerHost 是连接池。如果您输入 9000000,它将尝试创建 90000000 个线程和连接对象来处理您的请求。我相信这是远远超过必要的。您必须减少该数字,并且您还可以删除插入函数中的 sleep(100) 。

如果 java 尝试创建那么多连接。Mongodb 服务器可能无法处理那么多连接,或者您必须更改配置以允许那么多连接。

请记住,connectionsPerHost 不是最大线程数。您可以插入 1000000 个线程,但只有 100 个连接将被使用,并且一旦空闲,所有线程将共享它们。2 秒的 maxWaitTime 应该绰绰有余。如果插入时间超过 2 秒,您可能会考虑升级您的服务器。

于 2012-07-04T10:35:12.330 回答
1

驱动程序无法从池中的连接中删除丢弃的套接字,您需要使用 try catch 让驱动程序知道套接字已被丢弃。

于 2013-08-16T06:35:29.473 回答