Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring batch JdbcCursorItemReader cause a memory not enough issue

I am using the spring batch framework to do a data migration. The reader I use is JdbcCursorItemReader. I set chunk size as 500 and set the reader fetch size as 1000. But when run the service with spring batch it just seems to read all the data once in the memory and run out of the memory . then throw a memory not enough issue. Below is how I define the reader:

   private JdbcCursorItemReader<Map<String, Object>> buildItemReader(final DataSource dataSource, String tableName,String tenant) {
        String tenantName = tenantHelper.determineTenant(tableName);
        JdbcCursorItemReader<Map<String, Object>> itemReader = new JdbcCursorItemReader<>();
        itemReader.setDataSource(dataSource);
        itemReader.setSql("select * from " + tableName + " where " + tenantName + " ='" + tenant + "'");
        itemReader.setRowMapper(new ColumnMapRowMapper());
        itemReader.setFetchSize(100);
        return itemReader;
    }

Whats more, from the spring batch document here, we should be able to avoid the memory issue by using the jdbcCursorItemReader

like image 472
Ray Avatar asked Jul 23 '26 07:07

Ray


2 Answers

You can try using a JdbcPagingItemReader instead of JdbcCursorItemReader where the page size can be set while configuring it

like image 145
Anirudh Simha Avatar answered Jul 24 '26 23:07

Anirudh Simha


Figured this out by using the jdbcPagingItemReader. The root cause that the the cursor reader consume massive memory is because it just read all the data into the memory and then process it, which will be considered as a big object by the JVM and it will be allocated into the old generation directlly, Until the whole process finished, it could not be collected.

like image 34
Ray Avatar answered Jul 24 '26 22:07

Ray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!