51

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?

4
  • Why not just use the JDBC driver for MySQL? Commented May 6, 2009 at 18:34
  • Because it uses tcp/ip sockets instead of unix domain sockets? Commented May 6, 2009 at 19:14
  • 3
    The better question is "Why not enable TCP/IP and then use the JDBC driver for MySQL?", but sometimes we don't get to make that call :) Commented May 13, 2009 at 13:36
  • 4
    According to Stevens, Unix domain sockets are twice as fast as TCP/IP sockets Commented Sep 29, 2016 at 13:19

8 Answers 8

31

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

https://github.com/mcfunley/juds

Sign up to request clarification or add additional context in comments.

2 Comments

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, 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)
27

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.

3 Comments

also junixsocket is Apache 2.0 License, where as juds is LGPL
I can't recommend it. You are not able to stop the accept() method with closing the socket. Only through stopping the thread, which is probably a bad idea..
@xoned's issue has been fixed a while ago. Please give junixsocket another try. Thanks!
8

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.

2 Comments

But this is for TCP/IP over unix sockets only right? Does Java support Unix datagram sockets, so we can talk to SystemD for example? Lol chat gpt 4 says it can't, but I figured I'd check in with a human;)
@MohamedHafez Not yet. See: bugs.openjdk.org/browse/JDK-8297837
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.

Comments

3

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

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

2 Comments

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. :)
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
1

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

Comments

0

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

has worked for me

Comments

-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);
        }
    }
}

3 Comments

Both URLs lead to 404 as of now.
@GregDubicki fixed it up a bit.
Thanks, but both wayback links point to same URL. But please don't try to fix it - what is the point of linking to a lib which is so dead that its doc are not available anymore? How would you download it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.