本帖最后由 zhaorong 于 2018-2-25 13:05 编辑
总结下这周帮助客户解决报表生成操作的mysql 驱动的使用上的一些问题,与解决方案。
由于生成报表逻辑要从数据库读取大量数据并在内存中加工处理后再生成大量的
汇总数据然后写入到数据库。基本流程是 读取->处理->写入。
读取操作开始遇到的问题是当sql查询数据量比较大时候基本读不出来。开始以为是server端处理太慢。
但是在控制台是可以立即返回数据的。于是在应用这边抓包,发现也是发送sql后立即有数据返回。
但是执行ResultSet的next方法确实阻塞的。查文档翻代码原来mysql驱动默认的行为是需要把整个
结果全部读取到内存中才开始允许应用读取结果。显然与期望的行为不一致,期望的行为是流的方式读取,
当结果从myql服务端返回后立即还是读取处理。这样应用就不需要大量内存来存储这个结果集。
正确的流式读取方式代码示例:
- PreparedStatement ps = connection.prepareStatement("select .. from ..",
- ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
- //forward only read only也是mysql 驱动的默认值,所以不指定也是可以的 比如:
- PreparedStatement ps = connection.prepareStatement("select .. from ..");
- ps.setFetchSize(Integer.MIN_VALUE); //也可以修改jdbc url通过defaultFetchSize参数来设置,
- 这样默认所以的返回结果都是通过流方式读取.
- ResultSet rs = ps.executeQuery();
-
- while (rs.next()) {
- System.out.println(rs.getString("fieldName"));
- }
复制代码
代码分析:下面是mysql判断是否开启流式读取结果的方法,有三个条件forward-only,
read-only,fatch size是Integer.MIN_VALUE
- /**
- * We only stream result sets when they are forward-only, read-only, and the
- * fetch size has been set to Integer.MIN_VALUE
- *
- * @return true if this result set should be streamed row at-a-time, rather
- * than read all at once.
- */
- protected boolean createStreamingResultSet() {
- try {
- synchronized(checkClosed().getConnectionMutex()) {
- return ((this.resultSetType == java.sql.ResultSet.TYPE_FORWARD_ONLY)
- && (this.resultSetConcurrency == java.sql.ResultSet.CONCUR_READ_ONLY)
- && (this.fetchSize == Integer.MIN_VALUE));
- }
- } catch (SQLException e) {
- // we can't break the interface, having this be no-op in case of error is ok
-
- return false;
- }
- }
复制代码
|