Unable to instantiate Mongo related beans in Spring context when DNS records (SRV and TXT) are being used

Viewed 219

I was using without any Mongo beans instantiation issue a Java maven project with Java 8 and Spring Data MongoDb Reactive. Currently, I’m trying to upgrade the same project from Java 8 project to Java 11 (jvm 11.0.8+10-LTS). My updated spring boot version is 2.5.6 with Jetty server (9.4.44.v20210927), and the corresponding Mongo reactive version is based on 2.5.6. When Spring is trying to create the mongo related beans in order to add them in the context the following error appears:

Unable to support mongodb+srv// style connections as the ‘com.sun.jndi.dns.DnsContextFactory’ "
+ "class is not available in this JRE. A JNDI context is required for resolving SRV records

Among others I've tried the following:

add a jndi.properties file that sets a property

java.naming.provider.url=dns:.

add -exports argument to the maven compiler plugin

 <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                        <release>11</release>
                    <compilerArgs>
                        <arg>--add-exports</arg>
                        <arg>jdk.naming.dns/com.sun.jndi.dns=java.naming</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>

upgraded to the latest jetty server version compatible with java 11

 <jetty.version>9.4.44.v20210927</jetty.version>

<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-webapp</artifactId>
 <version>${jetty.version}</version>
 <scope>provided</scope>
  </dependency>

Can you please advise me with a solution?

1 Answers

I think you actually dont have to add --add-exports. If you look at the Mongodb Java driver code, you see it is already adding dns:

This was a PR https://github.com/mongodb/mongo-java-driver/pull/554

It might be JVM related, but hard to prove on this. Can you run it on a docker container using OpenJDK 11 and see? Or different versions?

    /*
      It's unfortunate that we take a runtime dependency on com.sun.jndi.dns.DnsContextFactory.
      This is not guaranteed to work on all JVMs but in practice is expected to work on most.
    */
    private static InitialDirContext createDnsDirContext() {
        Hashtable<String, String> envProps = new Hashtable<String, String>();
        envProps.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
        envProps.put(Context.PROVIDER_URL, "dns:");
        try {
            return new InitialDirContext(envProps);
        } catch (NamingException e) {
            throw new MongoClientException("Unable to support mongodb+srv// style connections as the 'com.sun.jndi.dns.DnsContextFactory' "
                    + "class is not available in this JRE. A JNDI context is required for resolving SRV records.", e);
        }
    }
}

https://github.com/mongodb/mongo-java-driver/blob/master/driver-core/src/main/com/mongodb/internal/dns/DefaultDnsResolver.java

Related