UNIX socket implementation for Java?

大城市里の小女人 提交于 2019-11-26 20:04:46
njsf

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.

Dave Cheney

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

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

Robert

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.

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

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);
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!