有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java是否可以通过PostgresSQL JDBC强制使用仅客户端准备的语句?

我希望在不更改代码的情况下消除PGSQL上服务器端准备的语句。这在JDBC上是可能的吗

明确地说,我希望使用客户机准备的语句,这意味着我的代码不会改变,我将享受准备语句的安全好处,但不会在服务器上编译它


共 (1) 个答案

  1. # 1 楼答案

    The documentation说:

    There might be cases when you would want to disable use of server-prepared statements. For instance, if you route connections through a balancer that is incompatible with server-prepared statements, you have little choice.

    You can disable usage of server side prepared statements by setting prepareThreshold=0

    代码示例:

    java.sql.PreparedStatement pstmt = conn.prepareStatement("SELECT ...");
    // get the PostgreSQL-specific class
    org.postgresql.PGStatement pgstmt = pstmt.unwrap(org.postgresql.PGStatement.class);
    // disable server-side prepared statements
    pgstmt.setPrepareThreshold(0);
    

    如果unwrapjava.sql.Connectionorg.postgresql.PGConnection,还可以在连接级别上为该连接上所有准备好的语句设置阈值

    将其添加到URL的示例:jdbc:postgresql://localhost/test?user=fred&password=secret&ssl=true&prepareThreshold=0