gpt4 book ai didi

java - 是否可以使用 JDBC 在 postgresql 登录流程的启动消息中传递 application_name?

转载 作者:行者123 更新时间:2023-11-29 13:39:00 26 4
gpt4 key购买 nike

我正在开发一个解析器,它可以嗅探网络流量并解析 PostgreSQL(服务器版本 9.6.11)的消息。我需要能够在登录流程中传递 application_name 参数(在它返回身份验证成功之前)这意味着我不能依赖“SET application_name”等。

这可以用 JDBC 实现吗?例如,使用 ODBC 可以通过设置 libpq 参数:application_name=myapp。启动消息 ( https://www.postgresql.org/docs/9.5/protocol-message-formats.html ) 将包含参数以及用户名和数据库。

我正在使用 Wireshark 来验证它。

我已尝试阅读有关 SO 的一些建议,但所有解决方案都依赖于在登录完成后设置应用程序名称。

我也尝试通过 DBeaver 的连接设置 -> 驱动程序属性 -> 应用程序名称,我得到了相同的结果。

Properties props = new Properties();
props.setProperty("user",user);
props.setProperty("password",password);
props.setProperty("ssl","true");
props.setProperty("sslmode","disable");
props.setProperty("ApplicationName","my-app");

try (Connection connection = DriverManager.getConnection("jdbc:postgresql://"+host+
":5432/" + db , props)) {

System.out.println("Java JDBC PostgreSQL Example");
Class.forName("org.postgresql.Driver");
System.out.println("Connected to PostgreSQL database!");
Statement statement = connection.createStatement();
}

或者这个,

Properties props = new Properties();
props.setProperty("user",user);
props.setProperty("password",password);
props.setProperty("ssl","true");
props.setProperty("sslmode","disable");

try (Connection connection = DriverManager.getConnection("jdbc:postgresql://"+host+
":5432/" + db+ "?application_name=my-app" , props)) {

System.out.println("Java JDBC PostgreSQL Example");
Class.forName("org.postgresql.Driver");
System.out.println("Connected to PostgreSQL database!");
Statement statement = connection.createStatement();
}

我正在寻找一种方法来复制 ODBC 行为。

这与 How to set application name in a Postgresql JDBC url? 不是同一个问题.我问是否可以在登录协议(protocol)完成之前传递此参数。当我使用 Wireshark 查看消息时,我可以清楚地看到使用 JDBC 传递此参数的所有方法都分解为以下消息序列:

  1. 登录请求
  2. 登录成功
  3. SET application_name = my-app

在 ODBC 中是这样的:

  1. 登录请求(包含参数application_name)
  2. 登录成功

最佳答案

在源代码中https://github.com/pgjdbc/pgjdbc/blob/master/pgjdbc/src/main/java/org/postgresql/core/v3/ConnectionFactoryImpl.java

具体从第311行开始:

private List<String[]> getParametersForStartup(String user, String database, Properties info) {
List<String[]> paramList = new ArrayList<String[]>();
paramList.add(new String[]{"user", user});
paramList.add(new String[]{"database", database});
paramList.add(new String[]{"client_encoding", "UTF8"});
paramList.add(new String[]{"DateStyle", "ISO"});
paramList.add(new String[]{"TimeZone", createPostgresTimeZone()});

Version assumeVersion = ServerVersion.from(PGProperty.ASSUME_MIN_SERVER_VERSION.get(info));

if (assumeVersion.getVersionNum() >= ServerVersion.v9_0.getVersionNum()) {
// User is explicitly telling us this is a 9.0+ server so set properties here:
paramList.add(new String[]{"extra_float_digits", "3"});
String appName = PGProperty.APPLICATION_NAME.get(info);
if (appName != null) {
paramList.add(new String[]{"application_name", appName});
}
} else {
// User has not explicitly told us that this is a 9.0+ server so stick to old default:
paramList.add(new String[]{"extra_float_digits", "2"});
}

application_name 添加到 paramList(如函数名所示,它是启动消息的参数列表)仅当 ASSUME_MIN_SERVER_VERSION 大于或等于 server-version: 9.0 时。此代码按我的预期工作:

        Properties props = new Properties();
props.setProperty("user",user);
props.setProperty("password",password);
props.setProperty("ssl","true");
props.setProperty("sslmode","disable");
props.setProperty(PGProperty.ASSUME_MIN_SERVER_VERSION.getName(),"9.6.11");
props.setProperty(PGProperty.APPLICATION_NAME.getName(),"my-app");
try (Connection connection = DriverManager.getConnection("jdbc:postgresql://"+host+
":5432/" + db+ "?ApplicationName=my-app" , props)) {

System.out.println("Java JDBC PostgreSQL Example");
Class.forName("org.postgresql.Driver");
System.out.println("Connected to PostgreSQL database!");
Statement statement = connection.createStatement();
}

主要变化:

props.setProperty(PGProperty.ASSUME_MIN_SERVER_VERSION.getName(),"9.6.11");

关于java - 是否可以使用 JDBC 在 postgresql 登录流程的启动消息中传递 application_name?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58202790/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com