admin管理员组

文章数量:1658606

参考文档
https://jdbc.postgresql/documentation/head/server-prepare.html
https://jdbc.postgresql/documentation/head/connect.html#autosave

连接相关设置

  • prepareThreshold(int):指定在一个会话中需要经过多少次PreparedStatement才对其进行服务器端PreparedStatement缓存,值为0表示禁用服务器端PreparedStatement缓存
  • preparedStatementCacheQueries(int):指定一个会话中最大缓存的PreparedStatement数量,默认值256,0表示禁用服务器端PreparedStatement缓存
  • preparedStatementCacheSizeMiB(int):最大缓存的PreparedStatement大小,默认为5M,0表示禁用服务器端PreparedStatement缓存
  • autosave(String):JDBC驱动42版本新增,设置当查询失败后如何处理
    • never: 默认值,不执行额外处理操作,直接抛出异常
    • always:每次查询前都会设置savepoint,如果出现错误则回滚到该检查点,并重新尝试执行
    • conservative:每次查询前都会设置savepoint,只有当出现 'cached statement cannot change return type’或’statement XXX is not valid’异常时才会回滚到检查点并重新尝试执行

PreparStatement缓存问题处理

服务器端缓存PreparedStatement后,如果表结构发生变化,再次使用PreparedStatement执行SQL将会出现错误,例如
  • 使用select * 查询,缓存后,添加列
  • 使用select 查询,修改列长度
Postgres的JDBC驱动对该问题进行了处理
  • 在非事物下,如果因此发生错误,JDBC驱动将会自动清除该缓存并重新执行,因此不会出现错误
  • 事物下,如果没有启动autosave,则执行抛出异常
  • 事物下,如果启用,JDBC驱动将会自动清除该缓存并重新执行,因此不会出现错误

事物下问题处理方案

方案一:JDBC驱动升级到42版本,并设置autosave为conservative

缺点:启用每次查询前设置savepoint,在超长事物时,会影响性能

方案二:连接释放到JDBC连接池前,执行“deallocate all”

当使用Druid连接池时,可以设置自定义Filter

public class ConnectionReleaseFilter extends FilterAdapter {

@Override
public void dataSource_releaseConnection(FilterChain chain, DruidPooledConnection connection) throws SQLException {
    if(!connection.isClosed()){
        connection.createStatement().executeUpdate("deallocate all");
    }
    chain.dataSource_recycle(connection);
}

}

本文标签: 报错语句DDLPreparStatementPostgres