Search code examples
javaspring-bootspring-ldappooling

LdapTemplate search catch exception with parallel stream and LdapTemplate


I has problems try go search groups on LDAP using spring-ldap search with parallelism.

I am make a batch processing to load data from SQL database to LDAP. I use spring-boot and spring-ldap.

My algorithm tries search for a group at ldap using its gidNumber. It works fine when run without parallelism (stream.parallelStream). But when I iterate a list with parallelStream I catch sometimes this exception:

java.lang.ClassCastException: class com.sun.jndi.ldap.LdapCtx cannot be cast to class org.springframework.ldap.core.DirContextAdapter (com.sun.jndi.ldap.LdapCtx is in module java.naming of loader 'bootstrap'; org.springframework.ldap.core.DirContextAdapter is in unnamed module of loader java.net.URLClassLoader @4f0f76b4)

I has cast the object from ContextMapper#mapFromContext to DirContextAdapter. Spring-ldap reference says this method should return a instance of DirContextAdapter if I does not change DirObjectFactory LdapContextSource.

My code is similar to:

ldapTemplate.search(
query().base(groupName()).where("gidNumber").is(Long.toString(gid)),
            (ContextMapper<GroupLdap>) ctx -> {
                DirContextAdapter context = (DirContextAdapter) ctx;
                return new GroupLdap(context, true);
            })

My application is a batch processing to load data from SQL database to LDAP. I use spring-boot and spring-ldap.

I'm using spring-boot 2.1.4, OpenLdap and ORACLE to SQL-DB. And open-jdk-zulu 11.

My algorithm tries search for a group at ldap using its gidNumber. It works fine when run without parallelism (stream.parallelStream). But when I iterate a list with parallelStream() and I catch sometimes a ClassCastException:

I put a ldap pool conection but it does not work.

My code is similar to:

    ldapTemplate.search(
    query().base(groupName()).where("gidNumber").is(Long.toString(gid)),
                (ContextMapper<GroupLdap>) ctx -> {
                    DirContextAdapter context = (DirContextAdapter) ctx;
                    return new GroupLdap(context, true);
                })

groupName is a Name with references to "ou=groups,dc=fff,dc=br";

gid is a number with gitNumber of searched group.

GroupLdap is a internal representation of group on LDAP.

With parallelism, i receive sometimes the exception:

java.lang.ClassCastException: class com.sun.jndi.ldap.LdapCtx cannot be cast to class org.springframework.ldap.core.DirContextAdapter (com.sun.jndi.ldap.LdapCtx is in module java.naming of loader 'bootstrap'; org.springframework.ldap.core.DirContextAdapter is in unnamed module of loader java.net.URLClassLoader @4f0f76b4)

I has cast the object from ContextMapper#mapFromContext to DirContextAdapter. Spring-ldap reference says this method should return a instance of DirContextAdapter if I does not change DirObjectFactory LdapContextSource.


Solution

  • We confirmed with team, changing to stream() from parallelStream() solves the problem of ClassCastException. It is definitely some openJdk defect. Can't find anything related on: https://bugs.openjdk.java.net. Once I reproduce isolated issue, i will report it. For now parallelStream is not always working (on few setups it working on few it doesn't)

    I use and can use parallelStream:

    Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T09:58:13+02:00) Maven home: C:\Program Files\apache-maven-3.5.2\bin.. Java version: 11.0.5, vendor: Oracle Corporation Java home: C:\Program Files\Java\jdk-11.0.5 Default locale: en_US, platform encoding: Cp1252 OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

    Colleague has, and experiencing issue: Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-04T21:00:29+02:00) Maven home: C:\tools\ideaIU-2019.2.3.win\plugins\maven\lib\maven3 Java version: 13.0.2, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk-13.0.2 Default locale: en_US, platform encoding: UTF-8 OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"