我在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();
}
}
答案 0 :(得分:4)
试试这个。
options.connectionsPerHost = 100;
options.maxWaitTime = 2000;
options.socketKeepAlive = true;
options.threadsAllowedToBlockForConnectionMultiplier = 50;
connectionsPerHost是connectionPool。如果您输入9000000,它将尝试创建90000000个线程和连接对象来处理您的请求。我认为这远远超过必要。您必须减少该数字,并且还可以删除插入函数中的sleep(100)。
如果java尝试创建那么多连接。 Mongodb服务器可能无法处理那么多连接,或者您必须更改配置以允许那么多连接。
请记住,connectionsPerHost不是最大线程数。您可以插入1000000个线程,但只使用100个连接,并且一旦它们空闲,将为所有线程共享。 2秒的maxWaitTime应该足够了。如果插入时间超过2秒,您可以考虑升级服务器。
答案 1 :(得分:1)
驱动程序无法从池连接中删除掉落的套接字,需要使用try catch让驱动程序知道套接字被丢弃。