Java 的 UNIX 套接字实现?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/170600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UNIX socket implementation for Java?
提问by Adam Bellaire
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.
我意识到由于 UNIX 套接字是特定于平台的,因此必须涉及一些非 Java 代码。具体来说,我们对使用 JDBC 连接到仅启用了 UNIX 域套接字的 MySQL 实例感兴趣。
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 ifwe can find a decent implementation of UNIX sockets for Java.
看起来这不受支持,但从我读到的内容来看,如果我们能找到适用于 Java 的 UNIX 套接字的体面实现,那么至少应该可以为基于 UNIX 套接字的 JDBC 编写 SocketFactory 。
Has anyone tried this? Does anyone know of such an implementation?
有没有人试过这个?有谁知道这样的实现?
采纳答案by njsf
Checkout the JUDS library. It is a Java Unix Domain Socket library...
查看 JUDS 库。它是一个 Java Unix Domain Socket 库...
回答by Dave Cheney
Check out the JNA library. It's a halfway house between pure Java and JNI native code
查看 JNA 库。它是纯 Java 和 JNI 本机代码之间的中途之家
回答by GWLlosa
Some searching on the internet has uncovered the following useful-looking library:
在互联网上的一些搜索发现了以下有用的库:
http://www.nfrese.net/software/gnu_net_local/overview.html
http://www.nfrese.net/software/gnu_net_local/overview.html
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);
}
}
}
回答by GWLlosa
You could use junixsocket: https://github.com/kohlschutter/junixsocket
您可以使用 junixsocket:https: //github.com/kohlschutter/junixsocket
It already provides code for connecting to MySQL from Java (Connector/J) via Unix sockets.
它已经提供了通过 Unix 套接字从 Java (Connector/J) 连接到 MySQL 的代码。
One big advantage compared to other implementations is that junixsocket uses the standard Java Socket API.
与其他实现相比的一大优势是 junixsocket 使用标准的 Java Socket API。
回答by Brett Okken
The JNR project(which is a loose basis for project panama) has a unix socketimplementation.
回答by Robert
The MariaDB JDBC drivernow supports this and is compatible with the MySQL JDBC driver.
该MariaDB的JDBC驱动程序现在支持这一点,是与MySQL JDBC驱动程序不兼容。
Use a JDBC url like:
使用 JDBC url,如:
jdbc:mariadb://localhost:3306/revmgt?localSocket=/var/run/mysqld/mysqld.sock
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.
值得注意的是,这个库需要包含 JNA 库,因为它使用 JNA 访问本机 unix 域套接字。它在我的测试中运行良好。我看到了 CPU 绑定 Java 进程从卸载到本机代码的速度改进。
回答by Greg Dubicki
As the original kohlschutter/junixsocket, mentioned in another answer seems to be dead, you can check out its forks.
由于在另一个答案中提到的原始kohlschutter/junixsocket似乎已死,您可以查看它的叉子。
Especially fiken/junixsocketlooks promising. Its author has added support for connection to PostgreSQL using unix socket via pgjdbc, for example.
尤其是fiken/junixsocket看起来很有希望。例如,它的作者添加了对使用 unix socket 通过pgjdbc连接到 PostgreSQL 的支持。