迁移DB到PPAS时,碰到的一个JDBC 问题的解决方法

本文涉及的产品
数据管理 DMS,安全协同 3个实例 3个月
推荐场景:
学生管理系统数据库
数据传输服务 DTS,数据迁移 small 3个月
推荐场景:
MySQL数据库上云
数据传输服务 DTS,数据同步 1个月
简介:

    在测试 PG/PPAS 的时候,一个很大的查询,总是会很快就报OOM。把heap dump出来以后,发现一个一个ResultSet占用了大概600M;而同样的代码在oracle上面就没有问题。然后google了一下,发现是(默认)PG会一次把 query 执行完,并把结果返回。

可以用如下的方式设置 fetchsize

http://jdbc.postgresql.org/documentation/head/query.html#fetchsize-example

Changing code to cursor mode is as simple as setting the fetch size of the Statement to the appropriate size. Setting the fetch size back to 0 will cause all rows to be cached (the default behaviour).

// make sure autocommit is off
conn.setAutoCommit(false);
Statement st = conn.createStatement();

// Turn use of the cursor on.
st.setFetchSize(50);
ResultSet rs = st.executeQuery("SELECT * FROM mytable");
while (rs.next())
{
   System.out.print("a row was returned.");
}
rs.close();

// Turn the cursor off.
st.setFetchSize(0);
rs = st.executeQuery("SELECT * FROM mytable");
while (rs.next())
{
   System.out.print("many rows were returned.");
}
rs.close();

// Close the statement.
st.close();


      同时,查询文档,发现 oracle jdbc(如果没有设置),默认是每次返回10条数据

目录
相关文章
|
5天前
|
关系型数据库 MySQL Java
启动项目出现com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException异常解决方法
启动项目出现com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException异常解决方法
|
关系型数据库 Java 应用服务中间件
|
Web App开发 关系型数据库 Java
Idea运行web项目时,提示java.lang.ClassNotFoundException: com.mysql.jdbc.Driver解决方法
今天用 idea写了个工程。结果最后报错,错误信息如下: java.lang.ClassNotFoundException: com.mysql.jdbc.Driverat org.apache.catalina.
3125 0
|
5天前
|
SQL Java 关系型数据库
MySQL之JDBC(二)
MySQL之JDBC(二)
36 0
|
5天前
|
关系型数据库 MySQL Java
MySQL之JDBC(一)
MySQL之JDBC
35 0
|
5天前
|
关系型数据库 MySQL Java
MySQL的主从复制 && SpringBoot整合Sharding-JDBC解决读写分离
MySQL的主从复制 && SpringBoot整合Sharding-JDBC解决读写分离
39 0

热门文章

最新文章