我有大型arrayList。我试图通过每插入1000个项目将所有列表插入mongodb。 但是,在插入了大约900 000条记录之后,MongoDb服务停止了。
我收到此错误:
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
Type: System.IO.IOException
Stack: at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at MongoDB.Bson.IO.BsonBuffer.LoadFrom(Stream stream, Int32 count)
at MongoDB.Bson.IO.BsonBuffer.LoadFrom(Stream stream)
at MongoDB.Driver.Internal.MongoConnection.ReceiveMessage[TDocument](BsonBinaryReaderSettings readerSettings, IBsonSerializationOptions serializationOptions)
at MongoDB.Driver.MongoCursorEnumerator`1.GetReply(MongoConnection connection, MongoRequestMessage message)
at MongoDB.Driver.MongoCursorEnumerator`1.GetFirst()
at MongoDB.Driver.MongoCursorEnumerator`1.MoveNext()
at MongoDB.Driver.MongoDatabase.GetCollectionNames()
at MangoUI.MCollections.GetAll(String db)
at MangoUI.MCollections.Get(String db)
at MangoUI.ComNavTree.ExpandMe(MTreeNode expand)
at MangoUI.ComNavTree.tree_BeforeExpand(Object sender, TreeViewCancelEventArgs e)
An existing connection was forcibly closed by the remote host
Type: System.Net.Sockets.SocketException
Stack: at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
我的代码是这样的。我该怎么办?
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package yaz;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import mudi.BOB;
import mudi.BOBM;
import mudi.Employee;
import tmsf.TMSF;
/**
*
* @author mkilic
*/
public class MongoDbInsert {
static List<BOBM> bobmList = new ArrayList<BOBM>();
static Mongo mongo;
static DB db;
static DBCollection employeeCollection;
public static void main(String[] args) throws UnknownHostException {
mongo = new Mongo("localhost", 27017);
db = mongo.getDB("TestDB");
employeeCollection = db.getCollection("tblBOB");
for (int j = 0; j < 13000; j++) {
bobmList = new ArrayList<BOBM>();
for (int i = 0; i < 1000; i++) {
BOBM bobm = new BOBM();
bobm.setB01("aaaaaaaaaaaaa1");
bobm.setB02("aaaaaaaaaaaaa2");
bobm.setB03("aaaaaaaaaaaaa3");
bobm.setB04("aaaaaaaaaaaaa4");
bobm.setB05("aaaaaaaaaaaaa5");
bobm.setB06("aaaaaaaaaaaaa6");
bobm.setB07("aaaaaaaaaaaaa7");
bobm.setB08("aaaaaaaaaaaaa8");
bobm.setB09("aaaaaaaaaaaaa9");
bobm.setB10("aaaaaaaaaaaaa10");
bobm.setB11("aaaaaaaaaaaaa11");
bobm.setB12("aaaaaaaaaaaaa12");
bobm.setB13("aaaaaaaaaaaaa13");
bobm.setB14("aaaaaaaaaaaaa14");
bobm.setB15("aaaaaaaaaaaaa15");
bobm.setB16("aaaaaaaaaaaaa16");
bobm.setB17("aaaaaaaaaaaaa17");
bobm.setB18("aaaaaaaaaaaaa18");
bobm.setB19("aaaaaaaaaaaaa19");
bobm.setB20("aaaaaaaaaaaaa20");
bobm.setB21("aaaaaaaaaaaaa21");
bobm.setB22("aaaaaaaaaaaaa22");
bobm.setB23("aaaaaaaaaaaaa23");
bobm.setB24("aaaaaaaaaaaaa24");
bobmList.add(bobm);
bobm = null;
}
insertMon(bobmList);
bobmList = null;
}
mongo.close();
}
public static void insertMon(List<BOBM> list) throws UnknownHostException {
for (Iterator<BOBM> it = list.iterator(); it.hasNext();) {
BOBM bobm = it.next();
employeeCollection.insert(bobm);
}
}
}