PostgresQL JDBC 是否有可能强制使用仅客户端准备好的语句?
Is it possible with PostgresSQL JDBC to force usage of client only Prepared Statements?
我想在不更改代码的情况下消除 PGSQL 上的服务器端准备语句。这在 JDBC 上可以通过某些参数实现吗?
明确地说,我想使用客户端准备语句,这意味着我的代码不会更改,我将享受准备语句的安全优势,但不会在服务器上编译它。
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);
如果您 unwrap
org.postgresql.PGConnection
在您的 java.sql.Connection
中,您还可以为该连接上的所有准备好的语句设置连接级别的阈值。
将其添加到 URL 的示例:jdbc:postgresql://localhost/test?user=fred&password=secret&ssl=true&prepareThreshold=0
我想在不更改代码的情况下消除 PGSQL 上的服务器端准备语句。这在 JDBC 上可以通过某些参数实现吗?
明确地说,我想使用客户端准备语句,这意味着我的代码不会更改,我将享受准备语句的安全优势,但不会在服务器上编译它。
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);
如果您 unwrap
org.postgresql.PGConnection
在您的 java.sql.Connection
中,您还可以为该连接上的所有准备好的语句设置连接级别的阈值。
将其添加到 URL 的示例:jdbc:postgresql://localhost/test?user=fred&password=secret&ssl=true&prepareThreshold=0