弹出批量在作业中大量选择后内存不足

时间:2018-02-02 18:00:54

标签: java mysql spring-batch

我的工作面临问题,

我试图从数据库中读取记录并写入txt文件。该数据库包含1.800.000条记录,包含149列,问题是select在jobConfig.xml中,在bean' mysqlItemReader'中,但是,我认为select尝试加载所有记录JVM内存然后我内存不足,使用randtb.cliente限制200000它运行正常,但超过500k的记录我内存不足,如何避免这个错误?谢谢!

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/batch 

  http://www.springframework.org/schema/batch/spring-batch-2.2.xsd 
  http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

<import resource="Context.xml" />
<bean id="tutorial" class="extractor.main.Tutorial" scope="prototype" />
<bean id="itemProcessor" class="extractor.main.CustomItemProcessor" />

<batch:job id="helloWorldJob">
    <batch:step id="step1">
        <batch:tasklet>
            <batch:chunk reader="mysqlItemReader" writer="flatFileItemWriter"
                processor="itemProcessor" commit-interval="50">
            </batch:chunk>
        </batch:tasklet>
    </batch:step>
</batch:job>

<bean id="mysqlItemReader"
    class="org.springframework.batch.item.database.JdbcCursorItemReader">
    <property name="dataSource" ref="dataSource"/>
    <property name="sql" value="select * from randtb.cliente"/>
    <property name="rowMapper">
        <bean class="extractor.main.TutorialRowMapper"/>
    </property>
</bean>

<bean id="flatFileItemWriter" class=" org.springframework.batch.item.file.FlatFileItemWriter">
    <property name="resource" value="file:target/outputfiles/employee_output.txt" />
    <property name="lineAggregator">
        <bean
            class=" org.springframework.batch.item.file.transform.PassThroughLineAggregator" />
    </property>
</bean>

2 个答案:

答案 0 :(得分:3)

默认情况下,MySql将返回ResultSet中导致OOM异常的所有内容。为了不这样做,您需要设置JdbcCursorItemReader#setFetchSize(Integer.MIN_VALUE)。这将告诉Spring Batch在PreparedStatement上设置该值以及设置PreparedStatement#setFetchDirection(ResultSet.FETCH_FORWARD)。这将告诉MySql流式传输数据,从而不会破坏你的堆栈。

因此,对于您的具体示例,您需要将ItemReader配置更改为:

<bean id="mysqlItemReader"
    class="org.springframework.batch.item.database.JdbcCursorItemReader">
    <property name="dataSource" ref="dataSource"/>
    <property name="sql" value="select * from randtb.cliente"/>
    <property name="fetchSize" value="#{T(java.lang.Integer).MIN_VALUE}"/>
    <property name="rowMapper">
        <bean class="extractor.main.TutorialRowMapper"/>
    </property>
</bean>

您可以在此处的文档中详细了解MySql的工作原理:https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-implementation-notes.html(参见ResultSet部分)。

答案 1 :(得分:0)

没有verifyCursorPosition,我得到了om.mysql.jdbc.RowDataDynamic$OperationNotSupportedException: Operation not supported for streaming result sets

但是,添加它,看起来有效

<bean id="mysqlItemReader"
class="org.springframework.batch.item.database.JdbcCursorItemReader">
<property name="dataSource" ref="dataSource"/>
<property name="sql" value="select * from randtb.cliente"/>
<property name="fetchSize" value="#{T(java.lang.Integer).MIN_VALUE}"/>
<property name="verifyCursorPosition" value="false"/>
<property name="rowMapper">
    <bean class="extractor.main.TutorialRowMapper"/>
</property>

相关问题