java大量数据加载时resultSetType参数的设置 TYPE_FORWARD_ONLY
来源:互联网 发布:linux管道实验 编辑:程序博客网 时间:2024/06/16 10:57
这两天的工作是将一1000万条数据库记录(10GB)加载到内存中,加载到500多万条记录的时候,就报了OOM(内存不足)。
解决了所有可能造成内存使用不当的外围问题后,再运行还是这个问题,无法加载全部数据。于是只好沉下心仔细研究了之前很少碰的底层访问数据库的代码(这部分代码是第三方的ORM软件通过模板生成的)。
/** * Load each the elements using a SQL statement specifying a list of fields to be retrieved and dealt by action. * @param sql the SQL statement for retrieving * @param argList the arguments to use fill given prepared statement,may be null * @param fieldList table of the field's associated constants * @param startRow the start row to be used (first row = 1, last row = -1)//起始行号 * @param numRows the number of rows to be retrieved (all rows = a negative number)//行数 * @param action Action object for do something(not null) * @return the count dealt by action * @throws DAOException */ public int loadBySqlForAction(String sql, Object[] argList, int[] fieldList,int startRow, int numRows,Action action) throws DAOException{ PreparedStatement ps = null; Connection connection = null; try { //获取数据库连接 connection = this.getConnection(); //创建PreparedStatement ps = connection.prepareStatement(sql, 1==startRow?ResultSet.TYPE_SCROLL_INSENSITIVE:ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); //填充PrepareStatement中的?占位符 fillPrepareStatement(ps, argList); //执行PreparedStatement return this.loadByPreparedStatement(ps, fieldList, startRow, numRows, action); } catch (DAOException e) { throw e; }catch (SQLException e) { throw new DataAccessException(e); } finally { this.getManager().close(ps); this.freeConnection(connection); } }
在创建PreparedStatement
时,resultSetType
参数设置的是TYPE_SCROLL_INSENSITIVE
或TYPE_SCROLL_SENSITIVE
,
这两个参数的共同特点是允许结果集(ResultSet
)的游标可以上下移动。而默认的TYPE_FORWARD_ONLY
参数只允许结果集的游标向下移动。
我加载这么大量的数据到内存过程中,只是顺序读取每一条记录,TYPE_FORWARD_ONLY
就够了,游标用不着前后移动,于是将改为TYPE_FORWARD_ONLY
,重新生成ORM代码,遂加载成功。
ps = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
总结
如果PreparedStatement
对象初始化时resultSetType
参数设置为TYPE_FORWARD_ONLY
,在从ResultSet
(结果集)中读取记录的时,对于访问过的记录就自动释放了内存。而设置为TYPE_SCROLL_INSENSITIVE
或TYPE_SCROLL_SENSITIVE
时为了保证能游标能向上移动到任意位置,已经访问过的所有都保留在内存中不能释放。所以大量数据加载的时候,就OOM了。
1 0
- java大量数据加载时resultSetType参数的设置 TYPE_FORWARD_ONLY
- java.sql.Connection.creatStatement(int resultSetType,int resultSetConcurrency)中参数的含义
- 多线程实现大量数据加载时的等待画面
- Java-针对查询大量数据时,解决效率的方案
- createStatement(int resultSetType, int resultSetConcurrency)参数意义
- createStatement(int resultSetType, int resultSetConcurrency)参数意义
- java集合中大量数据的匹配
- 关于easyui datagrid 大量数据加载时 出现假死问题的解决方法
- 从数据库中加载大量数据的方法
- UIScrollerView加载大量数据内存爆满的问题
- ListView&RecycleView加载大量数据的时候如何提高效率
- java写入大量数据
- java大量数据对比
- 大量数据时用到的AJAX分页。
- ResultSet 的Type属性 TYPE_FORWARD_ONLY, TYPE_SCROLL_I
- 大量数据的SQLLDR
- 大量数据的链接
- 数据集中参数的设置
- angularjs自定义表单验证email
- ionic 2 使用typescript
- 软件测试人员分工
- MyBatis的foreach语句详解
- 用git生成patch
- java大量数据加载时resultSetType参数的设置 TYPE_FORWARD_ONLY
- 拷贝赋值运算符处理自我赋值和异常安全检查
- C++ 静态存储区 栈 堆的区别
- bug的处理流程
- Android 触摸事件处理机制
- 从ramdisk根文件系统启动Linux
- 数据结构课程设计学生成绩管理系统源代码
- 简单的C程序<三>:单词计数、从输入行读取数据、最长行打印
- 数据结构课程设计—学生系统管理