50

I realize that since UNIX sockets are platform-specific, there has to be some non-Java code involved. Specifically, we're interested in using JDBC to connect to a MySQL instance which only has UNIX domain sockets enabled.

It doesn't look like this is supported, but from what I've read it should be at least possible to write a SocketFactory for JDBC based on UNIX sockets if we can find a decent implementation of UNIX sockets for Java.

Has anyone tried this? Does anyone know of such an implementation?

Vincent Cantin
  • 14,822
  • 2
  • 32
  • 55
Adam Bellaire
  • 104,025
  • 19
  • 146
  • 160

9 Answers9

30

Checkout the JUDS library. It is a Java Unix Domain Socket library...

https://github.com/mcfunley/juds

phihag
  • 263,143
  • 67
  • 432
  • 458
njsf
  • 2,699
  • 1
  • 21
  • 17
  • 1
    I am using juds right now to listen to UDS and i am receiving caught exceptionjava.io.IOException: Unable to open Unix domain socket. Any idea of what might be wrong? – Angel Nov 17 '15 at 05:12
  • 1
    @Angel, usually its cause of permission problems. Try to launch your application from root. If it helps - this is definitely permission problem (you need launch app from user having required permission) – iMysak Sep 23 '16 at 01:06
26

You could use junixsocket: https://github.com/kohlschutter/junixsocket

It already provides code for connecting to MySQL from Java (Connector/J) via Unix sockets.

One big advantage compared to other implementations is that junixsocket uses the standard Java Socket API.

Greg Dubicki
  • 4,819
  • 2
  • 48
  • 61
4

The MariaDB JDBC driver now supports this and is compatible with the MySQL JDBC driver.

Use a JDBC url like:

jdbc:mariadb://localhost:3306/revmgt?localSocket=/var/run/mysqld/mysqld.sock

Worth noting that this library require including the JNA library as it uses JNA to access native unix domain sockets. It works pretty well in my testing. I saw speed improvements on CPU bound java processes from the offload to native code.

Greg Dubicki
  • 4,819
  • 2
  • 48
  • 61
Robert
  • 131
  • 1
  • 2
4

As the original kohlschutter/junixsocket , mentioned in another answer seems to be dead, you can check out its forks.

Especially fiken/junixsocket looks promising. Its author has added support for connection to PostgreSQL using unix socket via pgjdbc, for example.

Greg Dubicki
  • 4,819
  • 2
  • 48
  • 61
4

As of Java 16, Unix domain sockets are supported natively by java through SocketChannel and ServerSocketChannel API.

You can find more information about it in JEP380 proposal and implementation example here.

Benny
  • 569
  • 5
  • 11
3

Check out the JNA library. It's a halfway house between pure Java and JNI native code

https://github.com/twall/jna/

GWLlosa
  • 23,392
  • 17
  • 77
  • 113
Dave Cheney
  • 5,405
  • 2
  • 17
  • 24
  • 1
    Thanks, Dave. It looks like we could use JNA to write our own socket implementation, then write a SocketFactory on top of it, though I was hoping to find something already written. :) – Adam Bellaire Oct 04 '08 at 16:34
  • Have a dig in the Jruby source, they use JNA to simulate a lot of pure ruby stuff including fork! There are also examples of a Posix class that should wrap most of the C level functions you need – Dave Cheney Oct 04 '08 at 16:49
1

The JNR project (which is a loose basis for project panama) has a unix socket implementation.

Brett Okken
  • 6,057
  • 1
  • 18
  • 24
0

no one has yet mentioned: https://github.com/sbt/ipcsocket

has worked for me

-2

Some searching on the internet has uncovered the following useful-looking library:

http://www.nfrese.net/software/gnu_net_local/overview.html

Wayback Link

Writing a socket factory should be easy enough. Once you've done so, you can pass it to your driver THUSLY.(Wayback Link).

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import com.mysql.management.driverlaunched.ServerLauncherSocketFactory;

public class ConnectorMXJTestExample {
    public static void main(String[] args) throws Exception {
        String hostColonPort = "localhost:3336";

        String driver = com.mysql.jdbc.Driver.class.getName();
        String url = "jdbc:mysql://" + hostColonPort + "/" + "?"
                + "socketFactory="
                + ServerLauncherSocketFactory.class.getName();
        String userName = "root";
        String password = "";

        Class.forName(driver);
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, userName, password);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT VERSION()");
            rs.next();
            String version = rs.getString(1);
            rs.close();
            stmt.close();

            System.out.println("------------------------");
            System.out.println(version);
            System.out.println("------------------------");
        } finally {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            ServerLauncherSocketFactory.shutdown(hostColonPort);
        }
    }
}
GWLlosa
  • 23,392
  • 17
  • 77
  • 113