gpt4 book ai didi

java - MongoDB:java.lang.IllegalStateException:状态应该是:打开

转载 作者:可可西里 更新时间:2023-11-01 09:29:04 51 4
gpt4 key购买 nike

我有一个应用程序可以从一个来源提取数据,然后将其更新插入到 MongoDB。有时,拉取过程会花费大量时间,当应用程序尝试将行推送到 MongoDB 时,可能会引发以下异常超时问题:

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)

MongoCollection 类有一个包装器,用于每个 MongoException,现在用于抛出的 java.lang.IllegalStateException

@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;
}

尝试重新加载 MongoClient:

        if ( mongoClient != null ) {
try {
mongoClient.close();
} catch (Exception e) {
Log.error("Mongo client throws exception while closing connection", e);
}
...
mongoClient = new MongoClient(servers, Arrays.asList(credential),
new MongoClientOptions.Builder()
.requiredReplicaSetName(REPLICASET_NAME)
.connectTimeout(0)
.connectionsPerHost(CONNECTION_PER_HOST)
.cursorFinalizerEnabled(false)
.build());

db = mongoClient.getDatabase(database);

但是,当 mongoClient 在“com.mongodb.MongoSocketReadException:过早到达流的末尾”之后更新时,它失败并显示:

java.lang.IllegalStateException: state should be: open
at com.mongodb.assertions.Assertions.isTrue(Assertions.java:70)
at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:82)
at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:75)
at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:71)
at com.mongodb.binding.ClusterBinding.getWriteConnectionSource(ClusterBinding.java:68)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:221)
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)

一直失败...

我使用 mongoDB ver 3.2.9;Mongo java 客户端 3.3.0

请多多支持

最佳答案

尝试在您的 MongoClientOptions 中设置 maxConnectionIdleTime 选项(特别是如果您使用的是托管 MongoDB 环境)

例子:

    new MongoClientOptions.Builder()
.requiredReplicaSetName(REPLICASET_NAME)
.maxConnectionIdleTime (MAX_IDLE_TIME)
.connectionsPerHost(CONNECTION_PER_HOST)
.cursorFinalizerEnabled(false)
.build();

connectTimeout 具有误导性,因为它是初始连接的超时设置。

关于重新初始化 MongoClient 的问题,请确保客户端代码在正确的范围内,以便插入代码能够获取。此外,我只看到数据库而不是集合级对象(代理?)被重新初始化。

关于java - MongoDB:java.lang.IllegalStateException:状态应该是:打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40670865/

51 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com