4

In Azure Cosmo DocumentDB in my application, Am getting following error

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)

I do not understand this one bit. The exception is occuring in the code that first checks if the collection exists (it does) then if it does not creates it. Clearly the create will fail, the collection exists!!

Create function

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());
}

Update Function :

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

Am getting issue while creating document.

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
user3647764
  • 41
  • 1
  • 3

2 Answers2

3

Use DocumentClient.UpsertDocumentAsync instead of documentClient.replaceDocumentAsync

Upsert will create a document if it doesn't already exist otherwise overwrite it. Replace requires that a document already exist and then overwrites it. Which to use is a concern of your application.

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • you can also read the doc here https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/cosmos-db/unique-keys.md – Sajeetharan Mar 27 '18 at 17:01
  • Thanks for quick response Sajeetharan, But am getting error in createDocument thanks for suggestion, I will update in replaceDocument. – user3647764 Mar 27 '18 at 18:49
  • Message: {"Errors":["Resource with specified id or name already exists."]} Am using java version,Am not able to find this method replaceDocumentAsync – user3647764 Mar 28 '18 at 07:04
1

if you are using ADF then use upsert in Sink, while you can do left outer join source with destination collection, its important to explicitly select sources right or left rather than allowing ADF to select it and check if id or any unique column is null in upsert condition in Alter Row

data flow steps

Sarang Kulkarni
  • 367
  • 2
  • 6