I use mongo-java-driver 3.1.0. There is no problem connection with JAVA and MongoDB. I can get data from MongoDB and write data into MongoDB by using JAVA.
But when I try to write more data, I get this fault on my java console. I think when socket connection lasts more than 30 seconds, MongoDB client crushes. Until 30 seconds some part of data is inserted to MongoDB (myCollection) First I get;
com.mongodb.MongoSocketReadException: Prematurely reached end of stream
and then
com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}
I did not set any timeout so its timeout should be infinity.
Can someone help
public void writeDB() {
MongoClient mongoClient = new MongoClient ("127.0.0.1", 27017);
mongoClient.setWriteConcern(WriteConcern.JOURNALED);
DB database = mongoClient.getDB("myCollection");
BasicDBObject newDataObject = new BasicDBObject();
DBCollection collection = database.getCollection("myCollection");
int number = 0;
for (int i = 0; i <= 1000000; i++) {
if(CHECKDATA_EXIST) {
int randomCount = (10 + (Math.random() * 300));
for (int j = 0; j < randomCount; j++) {
number = i + randomCount;
}
newDataObject.put("_id", i);
newDataObject.put("myNumber", number);
collection.insert(newDataObject);
}
}
}
By the way after crush I cannot write or get any data anymore. To write data, I have to restart "mongod".
I added CHECKDATA_EXIST method for if case (my code includes that). I forgot to add that. That method checks id is exist or not.