在spring boot中从postgresql导出数百万条记录到excel中。

问题描述 投票:0回答:1

我使用Apache poi jar和使用SXSSFWorkbook接口在spring boot java中导出百万条记录到excel。此外,我使用jdbc模板(fetchSize)和rowcallbackhandler从数据库(postgresql)中分块获取记录,但是看起来记录仍然被一次性从db中获取。请提出建议,以下是我使用的从db中获取记录的代码。

   jdbcTemplate.setFetchSize(1000);
   jdbcTemplate.query("select * from tbl",new 
                                     RowCallBackHandler(ResultSet rs){
                                           // Create row in excel and write data
                                      }
                                   );
postgresql spring-boot query-performance jdbctemplate
1个回答
0
投票

在我使用的一个类似的java代码中,我必须设置为

setAutoCommit(false)。

在连接中,为了让fetch正常工作。

       conn1 = DriverManager.getConnection(dbURL, dbUser, dbPass);
       conn1.setAutoCommit(false); 
       ...
        pstatement1 = conn1.prepareStatement(sqlsel);
        pstatement1.setString(1, sistema);
        pstatement1.setInt(2, id);
        pstatement1.setFetchSize(100);

祝你好运!如果这样还不能解决问题,请在你的问题中添加JAVA版本信息,以及驱动版本。

© www.soinside.com 2019 - 2024. All rights reserved.