I’ve spent three days on this so far, and I’m tearing my hair out. I’m trying to adapt some code I’ve inherited to migrate from AWS to DigitalOcean. Amazon’s DB didn’t require SSL to connect, so the code worked fine before.

I can connect with MySQL Workbench, but not within my code.

I’ve created a truststore and put the CA Certificate in it.

My test code is as follows:

import java.sql.SQLException;
import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;

public class DBTest {

    public static void main(String[] args) {

        MysqlConnectionPoolDataSource dataSource = new MysqlConnectionPoolDataSource();
            dataSource.setUser("adminusername");
            dataSource.setPassword("password");
            dataSource.setServerName("db-example-nyc1-01-do-user-123456-0.db.ondigitalocean.com");
            dataSource.setPort(25060);
            dataSource.setDatabaseName("defaultdb");
            // SSL Stuff
            dataSource.setTrustCertificateKeyStoreUrl("file:///opt/appname/truststore.store");
            dataSource.setTrustCertificateKeyStoreType("PKCS12");
            dataSource.setTrustCertificateKeyStorePassword("changeme");
            dataSource.setUseSSL(true);
            //dataSource.setRequireSSL(true);
        try {
            dataSource.getConnection();
            System.out.println("SUCCESS");
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            System.out.println("FAIL");
            e.printStackTrace();
        }
    }
}

(details changed for security where required)

The output in IntelliJ IDEA Community 2019.3 is:

4:13:13 PM: Executing task 'DBTest.main()'...

    > Task :compileJava NO-SOURCE
    > Task :processResources NO-SOURCE
    > Task :classes UP-TO-DATE
    > Task :compileTestJava
    > Task :processTestResources NO-SOURCE
    > Task :testClasses

    > Task :DBTest.main()
    Communications link failure

    Last packet sent to the server was 114 ms ago.
    FAIL

    BUILD SUCCESSFUL in 0s
    2 actionable tasks: 2 executed
    Picked up JAVA_TOOL_OPTIONS: -Djava.io.tmpdir=/home/baronofclubs/.var/app/com.jetbrains.IntelliJ-IDEA-Community/cache/tmp/
    com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

    Last packet sent to the server was 114 ms ago.
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
        at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
        at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2103)
        at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718)
        at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
        at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
        at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:422)
        at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:137)
        at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:107)
        at DBTest.main(DBTest.java:11)
    Caused by: java.lang.IllegalStateException: TrustManagerFactoryImpl is not initialized
        at java.base/sun.security.ssl.TrustManagerFactoryImpl.engineGetTrustManagers(TrustManagerFactoryImpl.java:102)
        at java.base/javax.net.ssl.TrustManagerFactory.getTrustManagers(TrustManagerFactory.java:313)
        at com.mysql.jdbc.ExportControlled.getSSLSocketFactoryDefaultOrConfigured(ExportControlled.java:237)
        at com.mysql.jdbc.ExportControlled.transformSocketToSSLSocket(ExportControlled.java:79)
        at com.mysql.jdbc.MysqlIO.negotiateSSLConnection(MysqlIO.java:4472)
        at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1319)
        at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2031)
        ... 13 more
    4:13:14 PM: Task execution finished 'DBTest.main()'.

I’m unsure what I’m doing wrong here. If anyone can provide any insight it would be extremely helpful.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hi @baronofclubs,

You’ll need to change your connection code like so

        MysqlConnectionPoolDataSource dataSource = new MysqlConnectionPoolDataSource();
            dataSource.setUser("adminusername");
            dataSource.setPassword("password");
            dataSource.setServerName("db-example-nyc1-01-do-user-123456-0.db.ondigitalocean.com:25060");
            dataSource.setDatabaseName("defaultdb");

The change is having the Port in the setServerName function.

If you additionally, try to connect and get an error like/or something similar as :

The server requested authentication method unknown to the client

Then you’ll need to create a new user with different authentication method that the default. To achieve this, you’ll need to connect via a Terminal to your MySQL server and use the CREATE USER command in there.

If you are having difficulties, you can check the following video here : https://www.youtube.com/watch?v=EAZoXK3i7eA&t=116s

Although it’s for PHP and MySQL Managed Database, the principle should be the same.

Regards,
KDSys

  • Unfortunately, no, this isn’t the issue. Defining a port in the URL String with MysqlConnectionPoolDatasource results in a duplication of the port definition in the URL. Or, if undefined via method, a default port being specified in the URL string. This is not the question. And neither is it a user issue. I’m connecting to the root user for debugging purposes.

    Primarily, I’m looking to solve why java.lang.IllegalStateException: TrustManagerFactoryImpl is not initialized is getting thrown here. How do I ensure the TrustManagerFactoryImpl gets initialized? Is there a reference or method call I’m missing?

    Thank you for your effort though.

Submit an Answer