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

标签 java apache-httpclient-4.x azure-cosmosdb

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

要插入数据,我循环遍历我的 pojo,并尝试像这样插入它们:

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

问题是,当我到达第一百个元素(这是最大连接池大小)时,我得到一个异常,即我的连接池无法给我另一个连接。 我也尝试修改连接池的设置,增加连接数据,减少“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 客户端中是否存在导致连接未释放的已知错误?我将不胜感激每一个帮助。

异常(exception):

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$1.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 是因为 DocumentClient 在调用 createXXXXXXXXX() 时不会自动关闭流。此行为旨在支持 createAttachment() 的流式 Blob 附件。

要关闭流,可以调用 close() 来关闭流,或者调用 .getResource() 返回资源,然后关闭流。

换句话说,替换以下行:

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

与:

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

或者:

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

关于java - 插入文档时达到最大池大小时出现 ConnectionPoolTimeoutException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29963218/

相关文章:

java - 获取十六进制数

java - Java中通过finalize()函数测试垃圾回收,是活的还是死的?

azure-cosmosdb - documentdb 创建请求消息时出错

azure - 无法更新索引策略

c# - 如果在使用 ConnectionMode.Direct 时不设置 ConnectionPolicy.ConnectionProtocol 会发生什么?

java - jSTL与spring框架3的问题

java - 第三个参数 Java8 reduce on Object 中的字符串属性

java - Apache HttpClient 4.4 代理基本身份验证 : Multiple auth attemps

java - Java 中 HTTP/1.1 持久 ("Connection: Close") 连接的最佳库是什么?

java - 为什么 Apache CloseableHttpResponse 在关闭时不使用实体?