{"id":21730,"date":"2024-01-02T14:21:25","date_gmt":"2024-01-02T14:21:25","guid":{"rendered":"https:\/\/server.hk\/blog\/21730\/"},"modified":"2024-11-14T10:27:56","modified_gmt":"2024-11-14T02:27:56","slug":"how-to-fix-mongodb-error-code-143-cursorinuse","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/21730\/","title":{"rendered":"\u5982\u4f55\u4fee\u5fa9 MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 143 &#8211; \u6e38\u6a19\u6b63\u5728\u4f7f\u7528\u4e2d"},"content":{"rendered":"<h1 id=\"how-to-fix-mongodb-error-code-143-cursorinuse-yHCOgtYxQc\">\u5982\u4f55\u4fee\u5fa9 MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 143 &#8211; \u6e38\u6a19\u6b63\u5728\u4f7f\u7528\u4e2d<\/h1>\n<p>MongoDB \u662f\u4e00\u500b\u6d41\u884c\u7684 NoSQL \u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u9ad8\u6027\u80fd\u3001\u53ef\u64f4\u5c55\u6027\u548c\u9748\u6d3b\u6027\u3002\u7136\u800c\uff0c\u50cf\u5176\u4ed6\u8edf\u4ef6\u4e00\u6a23\uff0c\u5b83\u53ef\u80fd\u6703\u9047\u5230\u6703\u5e72\u64fe\u6b63\u5e38\u904b\u4f5c\u7684\u932f\u8aa4\u3002\u5176\u4e2d\u4e00\u500b\u932f\u8aa4\u662f MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 143 &#8211; \u6e38\u6a19\u6b63\u5728\u4f7f\u7528\u4e2d\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u63a2\u8a0e\u6b64\u932f\u8aa4\u7684\u542b\u7fa9\u4ee5\u53ca\u5982\u4f55\u4fee\u5fa9\u5b83\u3002<\/p>\n<h2 id=\"understanding-mongodb-error-code-143-cursorinuse-yHCOgtYxQc\">\u7406\u89e3 MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 143 &#8211; \u6e38\u6a19\u6b63\u5728\u4f7f\u7528\u4e2d<\/h2>\n<p>\u7576\u60a8\u9047\u5230 MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 143 &#8211; \u6e38\u6a19\u6b63\u5728\u4f7f\u7528\u4e2d\u6642\uff0c\u9019\u610f\u5473\u8457\u60a8\u5617\u8a66\u8a2a\u554f\u7684\u6e38\u6a19\u5df2\u7d93\u5728\u4f7f\u7528\u4e2d\u3002\u6e38\u6a19\u662f\u6307\u5411\u67e5\u8a62\u7d50\u679c\u96c6\u7684\u6307\u91dd\uff0c\u5141\u8a31\u60a8\u904d\u6b77\u67e5\u8a62\u8fd4\u56de\u7684\u6587\u6a94\u3002\u6b64\u932f\u8aa4\u901a\u5e38\u767c\u751f\u5728\u60a8\u5617\u8a66\u5c0d\u5df2\u95dc\u9589\u6216\u5df2\u8017\u76e1\u7684\u6e38\u6a19\u57f7\u884c\u64cd\u4f5c\u6642\u3002<\/p>\n<p>\u6b64\u932f\u8aa4\u53ef\u80fd\u767c\u751f\u7684\u539f\u56e0\u6709\u5e7e\u500b\uff1a<\/p>\n<ul>\n<li><strong>\u6e38\u6a19\u8d85\u6642\uff1a<\/strong>MongoDB \u6703\u5728\u4e00\u5b9a\u6642\u9593\u5167\u81ea\u52d5\u95dc\u9589\u6e38\u6a19\uff0c\u4ee5\u91cb\u653e\u7cfb\u7d71\u8cc7\u6e90\u3002\u5982\u679c\u60a8\u5617\u8a66\u8a2a\u554f\u5df2\u95dc\u9589\u7684\u6e38\u6a19\uff0c\u5c07\u6703\u9047\u5230\u6b64\u932f\u8aa4\u3002<\/li>\n<li><strong>\u6e38\u6a19\u8017\u76e1\uff1a<\/strong>\u5982\u679c\u60a8\u5df2\u904d\u6b77\u6e38\u6a19\u4e2d\u7684\u6240\u6709\u6587\u6a94\uff0c\u518d\u6b21\u5617\u8a66\u8a2a\u554f\u5b83\u5c07\u5c0e\u81f4\u6b64\u932f\u8aa4\u3002<\/li>\n<li><strong>\u4e26\u767c\u554f\u984c\uff1a<\/strong>\u5982\u679c\u591a\u500b\u7dda\u7a0b\u6216\u9032\u7a0b\u540c\u6642\u8a2a\u554f\u540c\u4e00\u6e38\u6a19\uff0c\u53ef\u80fd\u6703\u5c0e\u81f4\u885d\u7a81\u4e26\u7522\u751f\u6e38\u6a19\u6b63\u5728\u4f7f\u7528\u4e2d\u7684\u932f\u8aa4\u3002<\/li>\n<\/ul>\n<h2 id=\"fixing-mongodb-error-code-143-cursorinuse-yHCOgtYxQc\">\u4fee\u5fa9 MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 143 &#8211; \u6e38\u6a19\u6b63\u5728\u4f7f\u7528\u4e2d<\/h2>\n<p>\u8981\u4fee\u5fa9\u6e38\u6a19\u6b63\u5728\u4f7f\u7528\u4e2d\u7684\u932f\u8aa4\uff0c\u60a8\u53ef\u4ee5\u9075\u5faa\u4ee5\u4e0b\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-check-cursor-state-yHCOgtYxQc\">1. \u6aa2\u67e5\u6e38\u6a19\u72c0\u614b<\/h3>\n<p>\u5728\u5c0d\u6e38\u6a19\u57f7\u884c\u4efb\u4f55\u64cd\u4f5c\u4e4b\u524d\uff0c\u6aa2\u67e5\u5176\u72c0\u614b\u4ee5\u78ba\u4fdd\u5b83\u662f\u958b\u653e\u7684\u4e14\u672a\u8017\u76e1\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>cursor.isClosed()<\/code> \u65b9\u6cd5\u4f86\u78ba\u5b9a\u6e38\u6a19\u662f\u5426\u5df2\u95dc\u9589\u3002\u5982\u679c\u5df2\u95dc\u9589\uff0c\u60a8\u9700\u8981\u91cd\u65b0\u67e5\u8a62\u6578\u64da\u5eab\u4ee5\u7372\u53d6\u65b0\u7684\u6e38\u6a19\u3002<\/p>\n<pre><code>if (cursor.isClosed()) {\r\n  cursor = db.collection.find(query);\r\n}<\/code><\/pre>\n<h3 id=\"2-handle-cursor-timeout-yHCOgtYxQc\">2. \u8655\u7406\u6e38\u6a19\u8d85\u6642<\/h3>\n<p>\u5982\u679c\u6e38\u6a19\u56e0\u8d85\u6642\u800c\u95dc\u9589\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>cursor.maxTimeMS()<\/code> \u65b9\u6cd5\u589e\u52a0\u6e38\u6a19\u8d85\u6642\u503c\u3002\u9019\u5c07\u9632\u6b62\u6e38\u6a19\u904e\u65e9\u95dc\u9589\u3002<\/p>\n<pre><code>cursor.maxTimeMS(60000); \/\/ \u5c07\u6e38\u6a19\u8d85\u6642\u8a2d\u7f6e\u70ba 60 \u79d2<\/code><\/pre>\n<h3 id=\"3-avoid-cursor-exhaustion-yHCOgtYxQc\">3. \u907f\u514d\u6e38\u6a19\u8017\u76e1<\/h3>\n<p>\u70ba\u4e86\u907f\u514d\u8017\u76e1\u6e38\u6a19\uff0c\u8acb\u78ba\u4fdd\u5728\u518d\u6b21\u5617\u8a66\u8a2a\u554f\u4e4b\u524d\u904d\u6b77\u7d50\u679c\u96c6\u4e2d\u7684\u6240\u6709\u6587\u6a94\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u5faa\u74b0\u904d\u6b77\u6e38\u6a19\uff0c\u76f4\u5230\u5b83\u8017\u76e1\u3002<\/p>\n<pre><code>while (cursor.hasNext()) {\r\n  Document document = cursor.next();\r\n  \/\/ \u8655\u7406\u6587\u6a94\r\n}<\/code><\/pre>\n<h3 id=\"4-handle-concurrency-issues-yHCOgtYxQc\">4. \u8655\u7406\u4e26\u767c\u554f\u984c<\/h3>\n<p>\u5982\u679c\u60a8\u6709\u591a\u500b\u7dda\u7a0b\u6216\u9032\u7a0b\u8a2a\u554f\u540c\u4e00\u6e38\u6a19\uff0c\u60a8\u9700\u8981\u540c\u6b65\u8a2a\u554f\u4ee5\u907f\u514d\u885d\u7a81\u3002\u4f7f\u7528\u9069\u7576\u7684\u540c\u6b65\u6a5f\u5236\uff0c\u4f8b\u5982\u9396\u6216\u4fe1\u865f\u91cf\uff0c\u4ee5\u78ba\u4fdd\u540c\u4e00\u6642\u9593\u53ea\u6709\u4e00\u500b\u7dda\u7a0b\u6216\u9032\u7a0b\u8a2a\u554f\u6e38\u6a19\u3002<\/p>\n<pre><code>synchronized (cursor) {\r\n  if (cursor.hasNext()) {\r\n    Document document = cursor.next();\r\n    \/\/ \u8655\u7406\u6587\u6a94\r\n  }\r\n}<\/code><\/pre>\n<h2 id=\"summary-yHCOgtYxQc\">\u7e3d\u7d50<\/h2>\n<p>MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 143 &#8211; \u6e38\u6a19\u6b63\u5728\u4f7f\u7528\u4e2d\u767c\u751f\u5728\u60a8\u5617\u8a66\u8a2a\u554f\u5df2\u95dc\u9589\u6216\u5df2\u8017\u76e1\u7684\u6e38\u6a19\u6642\u3002\u8981\u4fee\u5fa9\u6b64\u932f\u8aa4\uff0c\u60a8\u9700\u8981\u6aa2\u67e5\u6e38\u6a19\u72c0\u614b\u3001\u8655\u7406\u6e38\u6a19\u8d85\u6642\u3001\u907f\u514d\u6e38\u6a19\u8017\u76e1\u4ee5\u53ca\u8655\u7406\u4e26\u767c\u554f\u984c\u3002\u901a\u904e\u9075\u5faa\u9019\u4e9b\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u78ba\u4fdd MongoDB \u6578\u64da\u5eab\u7684\u5e73\u7a69\u548c\u7121\u932f\u8aa4\u904b\u884c\u3002<\/p>\n<p>\u5982\u9700\u53ef\u9760\u548c\u9ad8\u6027\u80fd\u7684 VPS \u4e3b\u6a5f\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e <a href=\"https:\/\/server.hk\">Server.HK<\/a>\u3002\u901a\u904e\u6211\u5011\u4e00\u6d41\u7684 VPS \u4e3b\u6a5f\u670d\u52d9\uff0c\u60a8\u53ef\u4ee5\u9ad4\u9a57\u865b\u64ec\u5c08\u7528\u4f3a\u670d\u5668\u7684\u5f37\u5927\u548c\u9748\u6d3b\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4fee\u5fa9 MongoDB \u932f\u8aa4\u4ee3&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-21730","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21730","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=21730"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21730\/revisions"}],"predecessor-version":[{"id":23660,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21730\/revisions\/23660"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=21730"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=21730"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=21730"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}