我有一个应用程序从一个源提取数据然后将其上传到MongoDB。有时拉动过程需要花费大量时间,当应用程序尝试将行推送到MongoDB时,可能会出现以下异常,可能是超时问题:
.wrap p {
color: red;
}
MongoCollection类有一个包装器,对于每个MongoException,现在抛出java.lang.IllegalStateException
<div class="wrap">
<div>
<p>TEXT</p>
</div>
<p>TEXT 2</p>
</div>
尝试重新加载MongoClient:
com.mongodb.MongoSocketReadException: Prematurely reached end of stream
at com.mongodb.connection.SocketStream.read(SocketStream.java:88)
at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:492)
at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:222)
at com.mongodb.connection.UsageTrackingInternalConnection.receiveMessage(UsageTrackingInternalConnection.java:105)
at com.mongodb.connection.DefaultConnectionPool$PooledConnection.receiveMessage(DefaultConnectionPool.java:438)
at com.mongodb.connection.WriteCommandProtocol.receiveMessage(WriteCommandProtocol.java:262)
at com.mongodb.connection.WriteCommandProtocol.execute(WriteCommandProtocol.java:104)
at com.mongodb.connection.UpdateCommandProtocol.execute(UpdateCommandProtocol.java:64)
at com.mongodb.connection.UpdateCommandProtocol.execute(UpdateCommandProtocol.java:37)
at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:168)
at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:289)
at com.mongodb.connection.DefaultServerConnection.updateCommand(DefaultServerConnection.java:143)
at com.mongodb.operation.MixedBulkWriteOperation$Run$3.executeWriteCommandProtocol(MixedBulkWriteOperation.java:481)
at com.mongodb.operation.MixedBulkWriteOperation$Run$RunExecutor.execute(MixedBulkWriteOperation.java:647)
at com.mongodb.operation.MixedBulkWriteOperation$Run.execute(MixedBulkWriteOperation.java:400)
at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:180)
at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:169)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:232)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:223)
at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:169)
at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:75)
at com.mongodb.Mongo.execute(Mongo.java:827)
at com.mongodb.Mongo$2.execute(Mongo.java:810)
at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:515)
at com.mongodb.MongoCollectionImpl.update(MongoCollectionImpl.java:508)
at com.mongodb.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:355)
at com.test.db.mongoDB.RetryingMongoCollection.updateOne(RetryingMongoCollection.java:909)
但是,在&#34; com.mongodb.MongoSocketReadException之后更新mongoClient时:过早地到达流的末尾&#34;它失败了:
@Override
public UpdateResult updateOne(Bson filter, Bson update,
UpdateOptions updateOptions) {
UpdateResult retVal = null;
final ExceptionHelper ex = new ExceptionHelper(CAConstants.RETRIES, ErrorType.ERROR, true);
boolean isDone = false;
while (!isDone) {
try {
retVal = proxied.updateOne(filter, update, updateOptions);
isDone = true;
} catch (final MongoException | IllegalStateException e) {
ex.logIfZero("Failed to launch MongoDB operation", e);
if (ex.lastTry()) {
isDone = true;
Log.error("Failed to launch MongoDB operation.", e);
} else {
Log.error("[MDB] Going to update mongo instance due to exception", e);
db = MongoDBFactory.getUpdatedMongoDBInstance();
setCollection();
}
}
}
return retVal;
}
并且一直都在失败......
我使用mongoDB ver 3.2.9; Mongo java客户端3.3.0
请支持。
答案 0 :(得分:3)
尝试在MongoClientOptions中设置maxConnectionIdleTime选项(特别是如果您使用的是托管的MongoDB环境)
示例:
new MongoClientOptions.Builder()
.requiredReplicaSetName(REPLICASET_NAME)
.maxConnectionIdleTime (MAX_IDLE_TIME)
.connectionsPerHost(CONNECTION_PER_HOST)
.cursorFinalizerEnabled(false)
.build();
connectTimeout具有误导性,因为它是初始连接的超时设置。
关于重新初始化MongoClient的问题,请确保客户端代码在插入代码的正确范围内。另外,我只看到数据库而不是重新初始化的集合级别对象(代理?)。