Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UNIX socket implementation for Java?

People also ask

Does java support Unix sockets?

As of Java 16, Unix domain sockets are supported natively java with SocketChannel / ServerSocketChannel API.

How are UNIX sockets implemented?

Unix sockets are created by socket sys call (while FIFO created by mkfifo). If you need client socket, you call connect, passing it server socket address. If you need server socket, you can bind to assign its address. While, for FIFO open call is used.


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

https://github.com/mcfunley/juds


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.


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.


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.


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

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