Azure documentDB - 具有指定 ID 或名称的资源已存在

标签 azure azure-cosmosdb document-database azure-java-sdk

在我的应用程序中的 Azure Cosmo DocumentDB 中,出现以下错误

2018-03-27 14:42:057 ERROR c.h.m.s.CosmosDBFruiteService - Could not add Fruites to Customer Reference : 11416e34-3620-45a4-b3be-b845bbf41762
Message: {"Errors":["Resource with specified id or name already exists."]} ActivityId: 1b70b944-d581-4640-8785-819400433bb4, Request URI: /apps/8d13f597-c7e4-4d60-8713-8a0e9abaa9ac/services/ce0d287f-0125-482b-b32c-22108b5b0ed3/partitions/42c80a49-8042-4032-8cfd-9937ecea8dcc/replicas/131662740073245648p, RequestStats: , SDK: Microsoft.Azure.Documents.Common/1.21.0.0, StatusCode: Conflict 2018-03-27 14:42:058 ERROR c.h.m.a.e.GlobalExceptionHandler - Exception: class org.apache.catalina.connector.ClientAbortException Message: java.io.IOException: An existing connection was forcibly closed by the remote host 2018-03-27 14:42:058 WARN o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Failed to invoke @ExceptionHandler method: public java.util.Map com.hm.myfeed.api.exception.GlobalExceptionHandler.handleException(java.lang.Exception,org.springframework.web.context.request.WebRequest,javax.servlet.http.HttpServletRequest) org.apache.catalina.connector.ClientAbortException: java.io.IOException: An existing connection was forcibly closed by the remote host at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:356) at org.apache.catalina.connector.OutputBuffer.flushByteBuffer(OutputBuffer.java:815) at org.apache.catalina.connector.OutputBuffer.append(OutputBuffer.java:720) at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:391) at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:369) at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:96) at org.springframework.security.web.util.OnCommittedResponseWrapper$SaveContextServletOutputStream.write(OnCommittedResponseWrapper.java:639)

这个我有点不明白。异常发生在代码中,该代码首先检查集合是否存在(存在),然后检查是否不创建它。显然创建将失败,集合存在!!

创建函数

try {
    fruitesDocument = documentClient.createDocument(getCollectionLink(), fruitesDocument , null, false).getResource();
} catch (DocumentClientException e) {
    LOG.error("Could not add fruits for Customer Reference: " + fruitesModel.getId() + "  " + e.getMessage());
}

更新功能:

try {
    fruitesDocument = documentClient.replaceDocument(fruitesDocument , null).getResource();
} catch (DocumentClientException e) {
    LOG.error("Error while updating fruites: " + e.getMessage());
}

创建文档时遇到问题。

最佳答案

使用DocumentClient.UpsertDocumentAsync而不是documentClient.replaceDocumentAsync

如果文档尚不存在,Upsert 将创建一个文档,否则将覆盖它。替换要求文档已经存在,然后覆盖它。使用哪一个是您的应用程序关心的问题。

关于Azure documentDB - 具有指定 ID 或名称的资源已存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49515670/

相关文章:

azure - cosmos documentDB 中是否存在带有搜索直到规则的查询

mongodb - 文档数据库中的链接与引用

windows - 使用 net use 在 Azure VM 中装载文件共享时出现 NativeCommandError

azure - 从 azure Linux VM 检索自定义数据

azure - 如何在 Azure 中 key 轮换后满足客户端对存储 Blob 的请求

c# - Windows azure REST API 列出容器问题

azure - CosmosDb 意外的延续 token

c# - 如何更新 DocumentDb 文档?

c# - 使用 R 对 DocumentDB 资源进行访问控制

azure - Cosmos DB SQL API 的架构迁移。说得通?