使用分页控件进行搜索会引发LDAPSearchException

时间:2017-01-11 11:20:43

标签: unboundid-ldap-sdk

我正在使用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()返回true,第二次迭代getConnectionPool().search(searchRequest);抛出LDAPSearchException(resultCode=2 (protocol error), numEntries=0, numReferences=0, errorMessage='paged results cookie is invalid')

我的代码有什么问题?

1 个答案:

答案 0 :(得分:1)

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

  1. 从池中获取连接,
  2. 使用此连接翻译结果,
  3. 将连接返回到池