'Spring LDAP failover instead of fallback

In a Spring-boot application we use spring-ldap to query an OpenLDAP server. The connection to the LDAP server is made like this:

    public LdapContextSource contextSource () {
        LdapContextSource contextSource= new LdapContextSource();
        contextSource.setUrl(env.getRequiredProperty("ldap.url"));
        contextSource.setBase(env.getRequiredProperty("ldap.base"));
        contextSource.setUserDn(env.getRequiredProperty("ldap.user"));
        contextSource.setPassword(env.getRequiredProperty("ldap.password"));
        return contextSource;
    }

The ldap.url contains: ldap://ldap1.example.com:389

Yesterday, I installed a second OpenLDAP instance and configured a Multi-Master replication. All is fine and my changes replicate correctly.

I changed the code to connect like this:

    public LdapContextSource contextSource () {
        LdapContextSource contextSource= new LdapContextSource();
        contextSource.setUrls(
                StringUtils.stripAll(
                        StringUtils.split(env.getRequiredProperty("ldap.urls"),',')));
        contextSource.setBase(env.getRequiredProperty("ldap.base"));
        contextSource.setUserDn(env.getRequiredProperty("ldap.user"));
        contextSource.setPassword(env.getRequiredProperty("ldap.password"));
        return contextSource;
    }

The ldap.urls contains: ldap://ldap1.example.com:389,ldap://ldap2.example.com:389

The actual behavior is a fall back mechanism:

  1. Both ldap1 and ldap2 are running
  2. I force a configuration change
  3. The java code does it on ldap1.
  4. ldap1 replicates to ldap2 within a few seconds. All is fine.
  5. I stop ldap1 and force a configuration change.
  6. The java code tries to do it on ldap1, timeout after a few seconds, does it on ldap2.
  7. I force a configuration change.
  8. The java code tries to do it on ldap1, timeout after a few seconds, does it on ldap2.
  9. I start ldap1. ldap2 replicates the pending changes. All is fine.
  10. I force a configuration change.
  11. The java code does it on ldap1.
  12. ldap1 replicates to ldap2 within a few seconds. All is fine.

The behavior I expect would be a fail-over:

  1. Both ldap1 and ldap2 are running
  2. I force a configuration change
  3. The java code does it on ldap1.
  4. ldap1 replicates to ldap2 within a few seconds. All is fine.
  5. I stop ldap1 and force a configuration change.
  6. The java code tries to do it on ldap1, timeout after a few seconds, does it on ldap2.
  7. I force a configuration change.
  8. The java code tries to do it on ldap2.
  9. I start ldap1. ldap2 replicates the pending changes. All is fine.
  10. I force a configuration change.
  11. The java code does it on ldap1.
  12. ldap1 replicates to ldap2 within a few seconds. All is fine.

I mean. I would like that once the ldap1 is detected as problematic, spring goes to ldap2 until ldap2 is problematic.

I tried to reduce timeout but did not succeed. I had a look at the pool notion but wasn't sure this was the right way to address this.

Thank you in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source