使用分页控件进行搜索会抛出 LDAPSearchException

Search with paged control throws LDAPSearchException

我正在使用 unboundid-ldapsdk-3.1.1 并尝试使用 SimplePagedResultsControl 遍历条目。

这是我正在使用的片段:

...
    searchRequest = new SearchRequest(dn, scope.getLdapSearchScope(), filter);    
    ASN1OctetString resumeCookie = null;
    while (true)
    {
        searchRequest.setControls(new Control[] { new SimplePagedResultsControl(searchLimit, resumeCookie) });
        setControls(searchRequest, controls);
        searchResult = getConnectionPool().search(searchRequest);
        numSearches++;
        totalEntriesReturned += searchResult.getEntryCount();
        for (SearchResultEntry e : searchResult.getSearchEntries()) {
            // Do something with each entry...
        }
        LDAPTestUtils.assertHasControl(searchResult, SimplePagedResultsControl.PAGED_RESULTS_OID);
        try {
            SimplePagedResultsControl responseControl = SimplePagedResultsControl.get(searchResult);
            if (responseControl.moreResultsToReturn())
                resumeCookie = responseControl.getCookie();
            else
                break;
        } catch (LDAPException ex) {
            log.error("Error while accessing cookies" + ex.getMessage());
        }
    }

当总条目数为 100 且 searchLimit 为 100 时,我不断收到错误消息:

第一次迭代 responseControl.moreResultsToReturn() returns true,第二次迭代 getConnectionPool().search(searchRequest); 抛出 LDAPSearchException(resultCode=2 (protocol error), numEntries=0, numReferences=0, errorMessage='paged results cookie is invalid')

我的代码可能有什么问题?

连接池通常有多个连接,并且可能 return 发出初始分页搜索结果请求的连接不是同一连接。解决方案是

  1. 从池中获取连接,
  2. 使用此连接浏览结果,
  3. return 到池的连接