プロが教える店舗&オフィスのセキュリティ対策術

すいません、、何方か知っている方がいましたらご教授下さい。

アプリ環境:windows2003 Websphere v5.1 JDK 1.4.2
LDAP環境:AIX5 OpenLDAP

の環境で、javaからLDAPにアクセスするロジックを作成中です。
アクセスは出来るのですが、コネクションがclose()を使っても切れません。
ガベージコレクションで切れる事はわかったのですが、何かほかの方法はないでしょうか?よろしくお願いいたします。


import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;



public class LdapDAO {

private Hashtable env;
private DirContext ctx = null;

public LdapDAO() throws NamingException {

String url = "ldap://172.22.1.85:389";
String auth = "simple";
String base = "ou=people,dc=com";
String user = "***";
String pass = "***";

env = new Hashtable();

env.put( Context.INITIAL_CONTEXT_FACTORY, cf);
env.put( Context.PROVIDER_URL, url);
env.put("java.naming.ldap.version", "3");
env.put( Context.SECURITY_AUTHENTICATION, auth);
env.put( Context.SECURITY_PRINCIPAL, user);
env.put( Context.SECURITY_CREDENTIALS, pass);
env.put("com.sun.jndi.ldap.connect.timeout", "3000");
env.put("com.sun.jndi.ldap.connect.pool", "true");
env.put("com.sun.jndi.ldap.connect.pool.timeout", "2000");
}

public String searchEmpAll(String employeeNumber, String flg) throws NamingException {
String result = "";

ctx = new InitialDirContext(env);

SearchControls cons = new SearchControls();
cons.setSearchScope(SearchControls.SUBTREE_SCOPE);
cons.setReturningObjFlag(true);
cons.setReturningAttributes(new String[]{****});

String filter = "****";
NamingEnumeration res = ctx.search(base, filter, cons);

for(int i = 0; res.hasMore(); i++) {
SearchResult entry = (SearchResult) res.next();
Attributes attrs = entry.getAttributes();
Attribute attribute = attrs.get(*****);

if(attribute != null) {
result = (String) attribute.get();
}
}

cons = null;
res = null;
ctx.close();
//System.gc();

return result;
}

}

A 回答 (3件)

LDAP通信も噛み砕けばソケットです。


ソケットなどの解放は、Javaのガベージコレクタが実装されなければ
ESTABLISHEDの状態のままです。
これはJavaの仕様です。
クローズ後、System.gc()を実装すればコネクションは消えます。

参考URL:http://support.microsoft.com/kb/231183/ja
    • good
    • 0

cons = null;


res.close(); ← この行を追加
res = null;
ctx.close();

これでコネクションが切れるはずです。

参考リンク)
http://www-1.ibm.com/support/entdocview.wss?rs=1 …
    • good
    • 0

res.close()


の後にctx.close()
ではいかがでしょうか?

この回答への補足

yama06様ご回答ありがとうございます。

res.close()してもダメでした。。

補足日時:2007/03/03 21:49
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!