public void save(List<Entity> entities) throws SQLException {

    try (

        Connection connection = database.getConnection(); // db 연결 파라미터 rewriteBatchedStatements=true 는 필수다

        PreparedStatement statement = connection.prepareStatement(SQL_INSERT);

    ) {

        int i = 0;


        for (Entity entity : entities) {

            statement.setString(1, entity.getSomeProperty());

            // ...


            statement.addBatch();

            i++;


            if (i % 5000 == 0 || i == entities.size()) {

                statement.executeBatch(); // 5000건 당 배치 실행

            }

        }

    }

}



한번 DBMS 호출에 여러 건의 DATA를 처리하는 방법으로 프로그램에서 다량 건 처리 시 db 호출 횟수를 획기적으로 줄여 

시스템 overhead 감소. 


약 10~15배 성능 향상 가능

+ Recent posts