ITeye博客

jdbc 批量提交

Fred_Han · 2012-09-28 19:11 · 数据库 · 数据库 · Markdown

场景:大批量新增数据

对比:逐笔提交和批次提交

 

记录数:2322条记录

测试结果:单笔提交:22938毫秒;批量提交:172毫秒

 

批量提交相关代码:

            connection = dataSource.getConnection();
            connection.setAutoCommit(false);
            // 组装新增sql
            String sql = assembleInsertSql();
            statement = connection
                    .prepareStatement(sql);
            for (int i = 0; i < beans.size(); i++) {
                // 组装statement参数
                aseembleStatement(statement, beans.get(i));
                statement.addBatch();
                // 每1000笔提交一次
                if (i % 1000 == 0) {
                    statement.executeBatch();
                    connection.commit();
                    statement.clearBatch();
                }
            }
            statement.executeBatch();
            connection.commit();

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。