简体   繁体   English

为什么从浏览器调用与从 Java 调用时 Cosmos 存储过程的运行方式不同?

[英]Why would a Cosmos stored procedure run differently when called from browser vs. called from Java?

I have a stored procedure in Cosmos DB Emulator.我在 Cosmos DB 模拟器中有一个存储过程。 All this procedure does is: delete ALL documents from mycollection .此过程所做的只是:从mycollection中删除所有文档。 When I run it in browser ( https://localhost:8081/_explorer/index.html ), it works great.当我在浏览器( https://localhost:8081/_explorer/index.html )中运行它时,它工作得很好。 Then I try to call it from Java:然后我尝试从 Java 调用它:

RequestOptions requestOptions = new RequestOptions(); 
requestOptions.setPartitionKey(new PartitionKey(null));
System.out.println("START DELETE PROCEDURE"); 
StoredProcedureResponse spr = client.executeStoredProcedure(sprocLink, requestOptions, null);     
System.out.println(spr.getResponseAsString()); 

And get the following result: {"deleted": 0,"continuation": false}并得到以下结果: {"deleted": 0,"continuation": false}

This is CRAZY.这太疯狂了。 I'm running this stored procedure from the browser and getting this result: {"deleted": 10,"continuation": false} .我正在从浏览器运行这个存储过程并得到这个结果: {"deleted": 10,"continuation": false} Then (of course adding back those 10 documents) running this result from Java and getting this result: {"deleted": 0,"continuation": false}然后(当然添加回这 10 个文档)从 Java 运行这个结果并得到这个结果: {"deleted": 0,"continuation": false}

So when the stored procedure is ran by Java, it is called but didn't do the job.因此,当存储过程由 Java 运行时,它被调用但没有完成工作。 Deleted nothing.... Why would this happen?什么都没删除......为什么会发生这种情况?


Below is the stored procedure下面是存储过程

   function testStoredProcedure( ) {
       var collection = getContext().getCollection();
        var collectionLink = collection.getSelfLink();
        var response = getContext().getResponse();
        var responseBody = {
            deleted: 0,
            continuation: true
        };

        var query = 'SELECT * FROM mycollection   ';

    // Validate input.
    if (!query) throw new Error("The query is undefined or null.");

    tryQueryAndDelete();

    // Recursively runs the query w/ support for continuation tokens.
    // Calls tryDelete(documents) as soon as the query returns documents.
    function tryQueryAndDelete(continuation)) {
        var requestOptions = {continuation: continuation};

        var isAccepted = collection.queryDocuments(collectionLink, query, requestOptions, function (err, retrievedDocs, responseOptions) {
            if (err) throw err;

            if (retrievedDocs.length > 0) {
                // Begin deleting documents as soon as documents are returned form the query results.
                // tryDelete() resumes querying after deleting; no need to page through continuation tokens.
                //  - this is to prioritize writes over reads given timeout constraints.
                tryDelete(retrievedDocs);
            } else if (responseOptions.continuation) {
                // Else if the query came back empty, but with a continuation token; repeat the query w/ the token.
                tryQueryAndDelete(responseOptions.continuation);
            } else {
                // Else if there are no more documents and no continuation token - we are finished deleting documents.
                responseBody.continuation = false;
                response.setBody(responseBody);
            }
        });

        // If we hit execution bounds - return continuation: true.
        if (!isAccepted) {
            response.setBody(responseBody);
        }
    }

    // Recursively deletes documents passed in as an array argument.
    // Attempts to query for more on empty array.
    function tryDelete(documents) {
        if (documents.length > 0) {
            // Delete the first document in the array.
            var isAccepted = collection.deleteDocument(documents[0]._self, {}, function (err, responseOptions) {
                if (err) throw err;

                responseBody.deleted++;
                documents.shift();
                // Delete the next document in the array.
                tryDelete(documents);
            });

            // If we hit execution bounds - return continuation: true.
            if (!isAccepted) {
                response.setBody(responseBody);
            }
        } else {
            // If the document array is empty, query for more documents.
            tryQueryAndDelete();
        }
    }
}

For partitioned containers, when executing a stored procedure, a partition key value must be provided in the request options.对于分区容器,在执行存储过程时,必须在请求选项中提供分区键值。 Stored procedures are always scoped to a partition key.存储过程始终限定为分区键。 Items that have a different partition key value will not be visible to the stored procedure.具有不同分区键值的项目对存储过程不可见。 This also applied to triggers as well.这也适用于触发器。

You are setting partition key to "null" in requestOptions.您在 requestOptions 中将分区键设置为“null”。 "null" is a valid partition key value. “null”是一个有效的分区键值。 Looks like "null" is not a partition key value for your 10 documents.看起来“null”不是您的 10 个文档的分区键值。

Humbly reposting @Jay Gong answer How to specify NONE partition key for deleting a document in Document DB java SDK?谦虚地转发@Jay Gong 回答如何指定NONE 分区键来删除文档DB java SDK 中的文档?

Maybe it will help someone.也许它会帮助某人。 Put:放:

PartitionKey partitionKey = new PartitionKey(Undefined.Value());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Java调用时,DB中的过程不起作用 - Procedure in DB not working when called from Java 从Java调用的Oracle存储过程中的日期格式 - Date format in an Oracle stored procedure called from Java 从Java应用程序调用时,存储过程不保留写入的表 - Stored procedure doesn't preserve the table written, when called from Java app 从 eclipse 与浏览器运行 jar - Run jar from eclipse vs. browser 为什么在Java中的线程对象上调用start()时没有立即调用run() - Why is run() not immediately called when start() called on a thread object in java 如何在从单独的Java程序调用的Microsoft SQL Server中调试存储过程? - How do you debug a stored procedure in a Microsoft SQL Server that's been called from a seperate java program? JPA调用存储过程从2 db中选择数据 - JPA called stored procedure selecting data from 2 db 为什么我的Python程序在从Java调用时会失败? - Why does my Python program fail when called from Java? 本地主机时如何重定向到本地存储的 index.html 文件:<port> 从浏览器调用</port> - How to redirect to locally stored index.html file when localhost:<port> is called from a browser 从Scala调用的Java方法运行缓慢 - Java methods called from scala run slowly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM