kohlschutter / junixsocket

Unix Domain Sockets in Java 7 and newer (AF_UNIX), AF_TIPC, AF_VSOCK, and more
Apache License 2.0
438 stars 114 forks source link

Socket connection fails from GraalVM native image #127

Closed Bharati17 closed 1 year ago

Bharati17 commented 1 year ago

I am trying to connect my local MySQL database, from a GraalVM native image using junixsocket

To Reproduce Steps to reproduce the behavior:

  1. Create a java file with Driver.getConnection using Connector/j and jdbc driver
  2. Include junix dependencies
  3. Create a jar with the dependencies and then create a GraalVM native image
  4. Run the image

Expected behavior MySQL database connection should be successful

Output/Screenshots java.sql.SQLNonTransientConnectionException: Cannot connect to MySQL server on localhost:3,306.

Make sure that there is a MySQL server running on the machine/port you are trying to connect to and that the machine this software is running on is able to connect to this host/port (i.e. not firewalled). Also make sure that the server has not been started with the --skip-networking flag.

at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:462)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
at java.sql@11.0.17/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql@11.0.17/java.sql.DriverManager.getConnection(DriverManager.java:189)
at com.oracle.mysql.test.LoadDriver.main(LoadDriver.java:33)

Caused by: java.lang.ClassCastException: com.mysql.cj.exceptions.CJException cannot be cast to com.mysql.cj.exceptions.UnableToConnectException at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825) at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:448) ... 5 more

Environment (please complete the following information):

Notes A normal jdbc connection without socket works. Am using 2.6.0 version of junixsocket and 22.3 version of GraalVM

kohlschuetter commented 1 year ago

Thanks for reporting, @Bharati17 !

I don't see junixsocket in the stack trace, it could be a bug in MySQL Connector. It could also be both (a bug in C/J that's triggered by a bug in junixsocket).

Most likely it has to do with something that's loaded via reflection and not properly configured for Native-Image. We can sort this out.

Does the socket connection work when you use TCP (i.e., not via junixsocket)?

Bharati17 commented 1 year ago

Hi kohlschuetter , Thank you for looking into it. The TCP Connection worked. However, socket connection failed. Like you mentioned, it was the metadata which was missing. Once I copied all the metadata(mentioned in https://kohlschutter.github.io/junixsocket/graalvm.html) jsons from junixcommon to GraalVM config, it worked like a charm. That was the missing piece. Closing this ticket.