Implementing a UNIX Socket for Java?

I understand that since UNIX sockets are platform specific, some non-Java code must be involved. In particular, we are interested in using JDBC to connect to a MySQL instance in which only UNIX sockets are included.

It doesn't seem like this is supported, but from what I read, it should at least be 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?

+45
java unix jdbc unix-domain-sockets
04 Oct '08 at 16:18
source share
7 answers

Place an order in the JUDS library. This is the Java Socket Socket Java Library ...

https://github.com/mcfunley/juds

+27
04 Oct '08 at 16:46
source share

You can 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 over other implementations is that junixsocket uses the standard Java Socket API.

+23
Sep 09 '09 at 6:07
source share

Check out the JNA library. This is halfway between pure Java and JNI native code.

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

+3
04 Oct '08 at 16:29
source share

Since the original kohlschutter / junixsocket mentioned in another answer seems to be dead, you can check its plugs.

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

+3
Mar 18 '17 at 12:25
source share

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

Use the JDBC URL, for example:

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

It is worth noting that you need to include the JNA library in this library, since it uses JNA to access the native sockets of the unix domain. This works very well in my testing. I have seen speed improvements on processors related to java processes from unloading to native code.

+2
Oct. 21 '16 at 15:40
source share

The following useful library has been found in some searches on the Internet:

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

Wayback link

Writing a factory socket should be fairly straightforward. Once you do this, you can pass it to your THUSLY driver. ( 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); } } } 
0
May 13 '09 at 2:52
source share

The JNR project (which is the free basis for the panama project ) has an unix socket implementation.

0
Jan 03 '15 at 16:50
source share



All Articles