插入文档时达到最大池大小时出现 ConnectionPoolTimeoutException

ConnectionPoolTimeoutException when reaching the max pool size while inserting documents

几天来我一直在使用 Azure DocumentDB,在插入数据时遇到了一个奇怪的行为。 (我使用 maven https://github.com/Azure/azure-documentdb-java 的 java sdk 的 1.0 版)

为了插入数据,我遍历了我的 pojos 并尝试像这样插入它们:

for (Order order : jsonImporter.getOrderList()) {
    Document doc = new Document(gson.toJson(order, Order.class));
    doc.setId(order.uuid);
    doc.set(TYPE, TYPE_ORDER);
    try {
        client.createDocument(getCollection().getSelfLink(), doc, null, true);
    } catch (DocumentClientException e) {
        System.err.printf("AzureDocumentDB - insertOrder request failed (order uuid %s)", order.uuid);
        System.err.println(e.getMessage());
    }
}

问题是,当我到达第 100 个元素(这是最大连接池大小)时,出现异常,我的连接池无法给我另一个连接。 我也试过修改连接池的设置,增加连接数据,减少"IdleConnectionTimeout"提早释放连接,但没有成功。
例如,当我将池大小增加到 500 时,在第 500 个元素之后出现异常。

ConnectionPolicy connectionPolicy = new ConnectionPolicy();
connectionPolicy.setMaxPoolSize(500);
connectionPolicy.setIdleConnectionTimeout(10);
client = new DocumentClient(END_POINT, MASTER_KEY, connectionPolicy, ConsistencyLevel.Session);

有人在我的代码中发现 API 误用吗?有人在插入数据时已经遇到过相同的行为吗?或者在 sdk 或 apache http 客户端中是否存在导致连接未被释放的已知错误?我将不胜感激。

异常:

java.lang.IllegalStateException: Http client execution failed.
    at com.microsoft.azure.documentdb.GatewayProxy.performPostRequest(GatewayProxy.java:350)
    at com.microsoft.azure.documentdb.GatewayProxy.doCreate(GatewayProxy.java:90)
    at com.microsoft.azure.documentdb.DocumentClient.doCreate(DocumentClient.java:1968)
    at com.microsoft.azure.documentdb.DocumentClient.createDocument(DocumentClient.java:456)
    ...
Caused by: org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool
    at org.apache.http.impl.conn.PoolingClientConnectionManager.leaseConnection(PoolingClientConnectionManager.java:226)
    at org.apache.http.impl.conn.PoolingClientConnectionManager.getConnection(PoolingClientConnectionManager.java:195)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:423)
    at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
    at com.microsoft.azure.documentdb.GatewayProxy.performPostRequest(GatewayProxy.java:347)

您看到 ConnectionPoolTimeoutException 是因为调用 createXXXXXXXX() 时 DocumentClient 不会自动关闭流。此行为旨在支持 createAttachment().

的流式 blob 附件

要关闭流,您可以调用close()关闭流或.getResource()到return资源然后关闭流。

换句话说,替换以下行:

client.createDocument(getCollection().getSelfLink(), doc, null, true);

与:

client.createDocument(getCollection().getSelfLink(), doc, null, true).close();

或者:

doc = client.createDocument(getCollection().getSelfLink(), doc, null, true).getResource();