[英]How to avoid an exception Prematurely reached end of stream using mongoDB Java driver 3.4+ or 3.6+? (during insertion)
我正在嘗試使用這段代碼將一些文檔插入到上限集合中:
// get document with specific fields
Document found = collection.find().first();
String getTitle = (String) found.get("title");
String getUrl = (String) found.get("url");
String getImg = (String) found.get("img");
String getPrice = (String) found.get("price");
// document which I want to get as new
Document doc = collection.find(new Document("title", getTitle)
.append("url", getUrl)
.append("img", getImg)
.append("price", getPrice)
.append("sent", true)).first();
// if the document doesn't exist, then insert as new
if (doc == null) {
collection.insertOne(new Document("title", getTitle)
.append("url", getUrl)
.append("img", getImg)
.append("price", getPrice)
.append("sent", true));
}
這意味着 - 重寫文件。 我插入的新文檔多了一個字段,而不是舊文檔,因為上限集合不允許用另一種大小更新現有文檔。 因為我得到一個例外: Cannot change the size of a document in a capped collection 。
舊文檔看起來像:
新的將是:
這段代碼工作正常,但過了一會兒(插入期間)我不斷收到錯誤消息:
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:491)
at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:221)
at com.mongodb.connection.CommandHelper.receiveReply(CommandHelper.java:134)
at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:121)
at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32)
at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:83)
at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:43)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
at com.mongodb.connection.UsageTrackingInternalConnection.open(UsageTrackingInternalConnection.java:46)
at com.mongodb.connection.DefaultConnectionPool$PooledConnection.open(DefaultConnectionPool.java:381)
at com.mongodb.connection.DefaultConnectionPool.get(DefaultConnectionPool.java:96)
at com.mongodb.connection.DefaultConnectionPool.get(DefaultConnectionPool.java:82)
at com.mongodb.connection.DefaultServer.getConnection(DefaultServer.java:72)
at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.getConnection(ClusterBinding.java:86)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:237)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:212)
at com.mongodb.operation.FindOperation.execute(FindOperation.java:482)
at com.mongodb.operation.FindOperation.execute(FindOperation.java:79)
at com.mongodb.Mongo.execute(Mongo.java:772)
at com.mongodb.Mongo$2.execute(Mongo.java:759)
at com.mongodb.FindIterableImpl$FindOperationIterable.first(FindIterableImpl.java:207)
at com.mongodb.FindIterableImpl.first(FindIterableImpl.java:148)
at project.Bot.onUpdateReceived(Bot.java:347)
據我正確理解,錯誤出現在行上(可能只是格式問題):
Document found = collection.find().first();
我研究並嘗試使用此代碼解決錯誤(我正在使用免費的 M0 層集群):
MongoCredential credential = MongoCredential.createCredential("admin1", "admin", "mypassword".toCharArray());
MongoClientSettings settings = MongoClientSettings.builder()
.credential(credential)
.retryWrites(true)
.applyToConnectionPoolSettings(builder ->
builder.maxConnectionIdleTime(60000, TimeUnit.MILLISECONDS))
.applyToSocketSettings(builder ->
builder.keepAlive(true))
.applyToSslSettings(builder -> builder.enabled(true))
.applyToClusterSettings(builder -> {
builder.hosts(Arrays.asList(
new ServerAddress("cluster0-shard-00-00-ox90k.mongodb.net", 27017),
new ServerAddress("cluster0-shard-00-01-ox90k.mongodb.net", 27017),
new ServerAddress("cluster0-shard-00-02-ox90k.mongodb.net", 27017)
));
builder.requiredReplicaSetName("Cluster0-shard-0");
})
.build();
MongoClient mongoClient = MongoClients.create(settings);
同樣的錯誤: com.mongodb.MongoSocketReadException: Prematurely reached end of stream
更新:嘗試而不是重寫整個文檔,只更改一個字段,如:
Document found = database.getCollection("capped_collection").find(new Document("title", title)
.append("url", url)
.append("img", img)
.append("price", price)).first();
if (found == null) {
collection.insertOne(new Document("title", title)
.append("url", url)
.append("img", img)
.append("price", price)
.append("sent", false));
Document doc = collection.find(eq("sent", false)).first();
if(doc != null){
collection.updateOne(eq("sent", false), new Document("$set", new Document("sent", true)));
}
但還是有:
com.mongodb.MongoSocketReadException: Prematurely reached end of stream
有誰知道我需要更正什么或如何解決錯誤Prematurely reached end of stream
?
我感謝任何幫助。
該錯誤通過連接格式解決(使用參數maxIdleTimeMS
、 ssl
和authSource
):
maxIdleTimeMS - 連接在被移除和關閉之前可以在池中保持空閑的最大毫秒數。
MongoClient mongoClient = MongoClients.create("mongodb://user:mypassword@cluster0-shard-00-00-ox90k.mongodb.net:27017,cluster0-shard-00-01-ox90k.mongodb.net:27017,cluster0-shard-00-02-ox90k.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true&maxIdleTimeMS=5000");
或者您可以以編程方式配置憑據(使用 java 驅動程序3.6+
版本):
admin
- 是在 Atlas 中定義用戶的數據庫;
user
- 是用戶名;
mypassword
- 是密碼;
MongoCredential credential = MongoCredential.createCredential("user", "admin", "mypassword".toCharArray());
MongoClientSettings settings = MongoClientSettings.builder()
.credential(credential)
.retryWrites(true)
.applyToConnectionPoolSettings(builder ->
builder.maxConnectionIdleTime(5000, TimeUnit.MILLISECONDS))
.applyToSslSettings(builder -> builder.enabled(true))
.applyToClusterSettings(builder -> {
builder.hosts(Arrays.asList(
new ServerAddress("cluster0-shard-00-00-ox90k.mongodb.net", 27017),
new ServerAddress("cluster0-shard-00-01-ox90k.mongodb.net", 27017),
new ServerAddress("cluster0-shard-00-02-ox90k.mongodb.net", 27017)
));
builder.requiredReplicaSetName("Cluster0-shard-0");
})
.build();
MongoClient mongoClient = MongoClients.create(settings);
定義主機名時,提及所有副本集主機名。
這將解決 java.net.UnknownHostException。
您不能像 mongodb+srv 連接那樣使用 DNS 短名稱。
而且,可以通過調用mongodb.MongoClient.connect
一次來解決錯誤,而不是每個請求。 嘗試重構您的代碼以在插入特定文檔期間調用連接一次而不是每次都調用。 在這種情況下,您可以避免選項 #1 中的任何其他參數。
剛剛夠:
mongodb+srv://admin:password@cluster0-ox90k.mongodb.net/test?retryWrites=true&w=majority
特別感謝mongoDB 支持。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.