рассматриваемый код: < /p>
Код: Выделить всё
// Set up environment for creating initial context
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:489/o=JNDITutorial");
// Set referral property to throw ReferralException
env.put(Context.REFERRAL, "throw");
try {
// Create initial context
DirContext ctx = new InitialDirContext(env);
// Set controls for performing subtree search
SearchControls ctls = new SearchControls();
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// Do this in a loop because we don't know how
// many referrals there will be
for (boolean moreReferrals = true; moreReferrals;) {
try {
// Perform search
NamingEnumeration answer = ctx.search("", "(objectclass=*)",
ctls);
// Print the answer
while (answer.hasMore()) {
System.out.println(">>>" +
((SearchResult)answer.next()).getName());
}
// search completes with no more referrals
moreReferrals = false;
} catch (ReferralException e) {
if (! followReferral(e.getReferralInfo())) {
moreReferrals = e.skipReferral();
}
// point to the new context
if (moreReferrals) {
ctx = (DirContext) e.getReferralContext();
}
}
}
// Close the context when we're done
ctx.close();
} catch (NamingException e) {
e.printStackTrace();
}
< /code>
Я считаю этот код ошибочным несколькими способами. Документация Dircontext Dircontext , созданный с помощью e.getReferContext () перезапись ctx , что означает, что промежуточные рефералы, а также инициализированные, а также initialdircontext никогда не закрыты и не приводят к утечке. /> ps: я также проверил внутреннюю реализацию Oracle в случае, который следует < /code>, и все забавные вещи - все контексты рефералов закрыты внутри. Мне посоветовали попробовать.
Подробнее здесь: https://stackoverflow.com/questions/363 ... -jndi-ldap