LDAP-101: Added eager invalidation of dead pooled connections.

This commit is contained in:
Mattias Hellborg Arthursson
2013-09-11 10:37:59 +02:00
parent 83b351c83c
commit 9687edf625
9 changed files with 324 additions and 11 deletions

View File

@@ -0,0 +1,87 @@
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.itest;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.support.LdapUtils;
import org.springframework.ldap.test.LdapTestUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
/**
* Tests the lookup methods of LdapTemplate.
*
* @author Mattias Hellborg Arthursson
* @author Ulrik Sandberg
*/
@ContextConfiguration(locations = {"/conf/ldapTemplatePooledTestContext.xml"})
public class LdapTemplatePooledITest extends AbstractJUnit4SpringContextTests {
@Autowired
private LdapTemplate tested;
@Autowired
@Qualifier("contextSourceTarget")
protected ContextSource contextSource;
@After
public void cleanup() throws Exception {
LdapTestUtils.shutdownEmbeddedServer();
}
/**
* This method depends on a DirObjectFactory (
* {@link org.springframework.ldap.core.support.DefaultDirObjectFactory})
* being set in the ContextSource.
*/
@Test
public void verifyThatInvalidConnectionIsAutomaticallyPurged() throws Exception {
LdapTestUtils.startEmbeddedServer(1888, "dc=jayway,dc=se", "jayway");
LdapTestUtils.cleanAndSetup(contextSource, LdapUtils.emptyLdapName(), new ClassPathResource("/setup_data.ldif"));
DirContextOperations result = tested.lookupContext("cn=Some Person2, ou=company1,c=Sweden");
assertEquals("Some Person2", result.getStringAttribute("cn"));
assertEquals("Person2", result.getStringAttribute("sn"));
assertEquals("Sweden, Company1, Some Person2", result.getStringAttribute("description"));
// Shutdown server and kill all existing connections
LdapTestUtils.shutdownEmbeddedServer();
LdapTestUtils.startEmbeddedServer(1888, "dc=jayway,dc=se", "jayway");
try {
tested.lookup("cn=Some Person2, ou=company1,c=Sweden");
fail("Exception expected");
} catch (Exception expected) {
// This should fail because the target connection was closed
assertTrue(true);
}
// But this should be OK, because the dirty connection should have been automatically purged.
tested.lookup("cn=Some Person2, ou=company1,c=Sweden");
}
}

View File

@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2005-2013 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<import resource="classpath:/conf/commonTestContext.xml" />
<bean id="contextSourceTarget"
class="org.springframework.ldap.core.support.LdapContextSource">
<property name="userDn" value="${userDn}" />
<property name="password" value="${password}" />
<property name="url" value="ldap://localhost:1888" />
<property name="base" value="dc=jayway,dc=se" />
</bean>
<bean id="contextSource" class="org.springframework.ldap.pool.factory.PoolingContextSource">
<property name="contextSource" ref="contextSourceTarget" />
<property name="maxActive" value="1" />
<property name="maxTotal" value="1" />
<property name="maxIdle" value="1" />
<property name="minIdle" value="1" />
<property name="testOnBorrow" value="false" />
<property name="testWhileIdle" value="false" />
</bean>
<bean id="ldapTemplate"
class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="contextSource" />
</bean>
<!--
<bean id="dataLoader" class="org.ddsteps.data.excel.CachingExcelDataLoader" />
-->
</beans>