【发布时间】:2016-11-20 17:19:13
【问题描述】:
关闭 Tomcat 后,我们看到很多关于内存泄漏的错误,因为 Tomcat 未能停止线程。 根据 Tomcat,我们有大约 2600 个守护线程在 com.sun.jndi.ldap.Connection.pauseReader 中等待。
我们正在使用 LdapTemplate 从 LDAP 读取数据。每次我们需要从 LDAP 读取数据时都会创建 LdapTemplate。从文档中我看到所有资源都是在搜索结束后由 LdapTemplate 释放的。 我们没有为 LdapTemplate 启用池化,默认为 false。
经过调试发现,为Connection创建的线程并没有在搜索结束后立即被销毁,但一些线程最终被销毁了。
知道为什么会有这么多守护线程等待 com.sun.jndi.ldap.Connection.pauseReader 吗?
我们正在使用 spring-ldap 2.0.2.RELEASE。
关于我们如何在执行搜索之前创建 LdapTemplate 的示例代码:
LdapContextSource contextSource = new LdapContextSource();
SimpleAuthenticationSource authenticationSource = new SimpleAuthenticationSource(userDn, password);
contextSource.setAuthenticationSource(authenticationSource);
LdapTemplate ldapTemplate = new LdapTemplate(contextSource);
ldapTemplate.setIgnorePartialResultException(true);
更新: 设置池化为 true 后,连接释放正常。
ldapContextSource.setPooled(true);
问题是我们不能使用池化。我仍然不明白为什么不使用池时连接没有正确释放。
【问题讨论】:
-
你能举个例子说明你是如何使用 LdapTemplate 的,Spring 版本之类的东西,它会有所帮助
标签: java jndi spring-ldap