2014-11-21 71 views
-1

我们用下面的LDAP JNDI连接池的设置:是上下文需要LDAP连接池被关闭显式

DirContext ctx = null; 
    try 
    { 
     Hashtable<String, String> env = new Hashtable<String, String>(); 

     env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 
     env.put(Context.PROVIDER_URL, "ldaps://" + server + ":" + serverPort); 
     env.put(Context.SECURITY_AUTHENTICATION, "simple"); 
     env.put(Context.SECURITY_PRINCIPAL, pUserName); 
     env.put(Context.SECURITY_CREDENTIALS, pPassword); 
     env.put(LdapContext.CONTROL_FACTORIES, "com.sun.jndi.ldap.ControlFactory"); 
     env.put(Context.SECURITY_PROTOCOL, "ssl"); 
     env.put("com.sun.jndi.ldap.read.timeout", "300000"); 

     Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 

     // load the location of keystore that holds trusted root certificates from web.xml 
     ServletContext context = ApplicationServlet.getApplication().getServlet().getServletContext(); 
     String certificatePath = context.getInitParameter("AD_CERTIFICATE_PATH"); 

     System.setProperty("javax.net.ssl.trustStore", certificatePath); 
     //   System.setProperty("javax.net.debug", "all"); 

     // For connection pooling 
     env.put("com.sun.jndi.ldap.connect.pool", "true"); 
     System.setProperty("com.sun.jndi.ldap.connect.pool.protocol", "plain ssl"); 
     System.setProperty("com.sun.jndi.ldap.connect.pool.maxsize", poolMaxSize); 
     System.setProperty("com.sun.jndi.ldap.connect.pool.prefsize", poolPrefSize); 
     System.setProperty("com.sun.jndi.ldap.connect.pool.timeout", poolTimeOut); 
     System.setProperty("com.sun.jndi.ldap.connect.pool.debug", "fine"); 

     ctx = new InitialDirContext(env); 
     return (DirContext) ctx; 

明确使用ctx.close()释放进行中,我们关闭背景下Active Directory搜索后连接回池。

有了上面的实现我们所面临的问题是连接越来越连接后立即关闭:

12:06:14,837 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-22) Create and use [email protected][eun1p3-be.stp-prod.st.com:636] 
12:06:16,855 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-22) Close [email protected] 
12:06:18,301 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-23) Create and use [email protected][eun1p3-be.stp-prod.st.com:636] 
12:06:20,353 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-23) Close [email protected] 
12:06:21,713 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-19) Create and use [email protected][eun1p3-be.stp-prod.st.com:636] 
12:06:23,746 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-19) Close [email protected] 
12:06:25,366 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-6) Create and use [email protected][eun1p3-be.stp-prod.st.com:636] 
12:06:27,473 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-6) Close [email protected] 
12:06:28,757 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-25) Create and use [email protected][eun1p3-be.stp-prod.st.com:636] 
12:06:30,855 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-25) Close [email protected] 
12:06:32,214 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-22) Create and use [email protected][eun1p3-be.stp-prod.st.com:636] 
12:06:34,294 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-22) Close [email protected] 
12:06:35,730 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-23) Create and use [email protected][eun1p3-be.stp-prod.st.com:636] 
12:06:37,753 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-23) Close [email protected] 
12:06:39,184 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-19) Create and use [email protected][eun1p3-be.stp-prod.st.com:636] 
12:06:41,266 ERROR [stderr] (http-eul151.sgp.st.com-10.75.32.13-10080-19) Close [email protected] 
+0

可能重复[每次从Ldap连接池返回新的连接](http://stackoverflow.com/questions/27057672/every-time-new-connection-is-returned-from- ldap-connection-pool) – 2014-11-21 13:24:13

+0

是的,我在同一个代码上询问了不同的问题。在这里,我面临的问题连接立即关闭。 – 2014-11-21 13:31:39

+0

大家好,我已经检查并没有得到太多的互联网。根据oracle文档提到,上下文需要关闭才能将其返回到池中。 – 2014-11-21 18:35:35

回答

2

是的,你不但所有Contexts也全部NamingEnumerations关闭。这就是连接返回池的方式。

您的日志中没有证据显示连接正在关闭。什么是关闭是com.sun.jndi.ldap.LdapClients.

+0

谢谢EJP,我明白了你的观点。我将关闭所有NamingEnumerations。 – 2014-11-22 09:46:20

+0

还有一点需要澄清,我们在NamingEnumerations上调用HasMoreElements()并纠正我,如果我错误地认为如果没有更多元素要从NamingEnumeration返回,那么HasMoreElements()方法需要隐式关闭NamingEnumeration。 – 2014-11-22 10:03:41

+0

支持参考:http://bugs.java.com/bugdatabase/view_bug.do?bug_id=5084229 – 2014-11-22 10:33:02