title: Spring Batch Perfomance is less compared to bufferedReader and JDBC Batch Inserts why?
body: <p>I have a file with 80 String columns, with approx 2 Lacs record in it.
  I was evaluating technology to be used as in future we will be getting 2000 Lacs record file.
  I have evaluated 2 things</p>
  
  <p>Oracle 11 g Database</p>
  
  <p><strong>Approach 1 - Spring Batch</strong></p>
  
  <p>Configuration of that is as below</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:batch="http://www.springframework.org/schema/batch" xmlns:task="http://www.springframework.org/schema/task"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      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"&gt;
  
      &lt;batch:job id="trialDataLoadJob"&gt;
          &lt;batch:step id="step1"&gt;
              &lt;batch:tasklet transaction-manager="transactionManager"&gt;
                  &lt;batch:chunk reader="trialInputFileReader" writer="trialJdbcWriter" commit-interval="1000"/&gt;
              &lt;/batch:tasklet&gt;
          &lt;/batch:step&gt;
      &lt;/batch:job&gt;
  
      &lt;bean id="trialInputFileReader" class="org.springframework.batch.item.file.FlatFileItemReader"  scope="step"&gt;
          &lt;property name="resource" value="file:#{jobParameters[inputFile]}" /&gt;
          &lt;property name="linesToSkip" value="1" /&gt;
          &lt;property name="lineMapper"&gt;
              &lt;bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper"&gt;
                  &lt;property name="lineTokenizer" ref="trialLineTokenizer" /&gt;
                  &lt;property name="fieldSetMapper" ref="trialFieldSetMapper" /&gt;
              &lt;/bean&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
  
      &lt;bean id="trialLineTokenizer" class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"&gt;
          &lt;property name="delimiter" value="," /&gt;
          &lt;property name="names"
              value="RUN_DATE,ACODE,GMI_ACCOUNT,GMI_OFFICE,GMI_FIRM,ACCOUNT_NAME,CCY,LE_ID,MARKET,GMF,MIC_CODE,GMI_PRD,PRD,PRD_DESCRIPTION,BBG_BACK_OFFICE_TICKER,BBG_FRONT_OFFICE_TICKER,BBG_YELLOW_KEY,ROOT_RIC,RIC,TRADE_TYPE,PROMPT_DATE,EXPIRY_DECLARATION,LAST_TRADED_DATE,STRIKE_PRICE,OPTION_TYPE,DELIVERY_TYPE,TRADE_PRICE,DECIMAL_TRADE_PRICE,TRIAL_TRADEPRICE,CONTRACTS,NOTIONAL_FOR_FUTURES,CLEARING_BROKER,EXEC_BROKER,TRADE_DATE,TRANSACTION_TYPE,UNDERLYING,UNDERLYING_TYPE,GMI_MULTIPLIER,UTI,USI,ISIN,AII,CFI,SERIAL,DEALER_REFERENCE,TRADE_EXECUTION_ID,CLEARING_TIMESTAMP,EXECUTION_TIMESTAMP,CCP_LE_ID,SWAP_TYPE,EFFECTIVE_DATE, COUPON_RATE,DAY_COUNT_BASIS,ROLL_FREQUENCY,RESET_FREQUENCY,ACTIVITY_TYPE,EMPTY,PRD_ID_PREFIX_1,PRD_ID_PREFIX_2,TRIAL_ENTITY_NAME,TRIAL_ENTITY_LEI,TRIAL_REGION" /&gt;
          &lt;property name="strict" value="false"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="demo" class="com.mkyong.Demo"&gt;&lt;/bean&gt;
  
  
          &lt;bean id= "trialFieldSetMapper" class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper"&gt;
              &lt;property name="prototypeBeanName" value="demo" /&gt;
          &lt;/bean&gt;
          &lt;bean id="trialJdbcWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
  
          &lt;property name="sql"&gt;
              &lt;value&gt;
              &lt;![CDATA[        
              Insert into TB_TRANS(RUN_DATE,ACODE,GMI_ACCOUNT,GMI_OFFICE,GMI_FIRM,ACCOUNT_NAME,CCY,LE_ID,MARKET,GMF,MIC_CODE,GMI_PRD,PRD,PRD_DESCRIPTION,BBG_BACK_OFFICE_TICKER,BBG_FRONT_OFFICE_TICKER,BBG_YELLOW_KEY,ROOT_RIC,RIC,TRADE_TYPE,PROMPT_DATE,EXPIRY_DECLARATION,LAST_TRADED_DATE,STRIKE_PRICE,OPTION_TYPE,DELIVERY_TYPE,TRADE_PRICE,DECIMAL_TRADE_PRICE,TRIAL_TRADEPRICE,CONTRACTS,NOTIONAL_FOR_FUTURES,CLEARING_BROKER,EXEC_BROKER,TRADE_DATE,TRANSACTION_TYPE,UNDERLYING,UNDERLYING_TYPE,GMI_MULTIPLIER,UTI,USI,ISIN,AII,CFI,SERIAL,DEALER_REFERENCE,TRADE_EXECUTION_ID,CLEARING_TIMESTAMP,EXECUTION_TIMESTAMP,CCP_LE_ID,SWAP_TYPE,EFFECTIVE_DATE, COUPON_RATE,DAY_COUNT_BASIS,ROLL_FREQUENCY,RESET_FREQUENCY,ACTIVITY_TYPE,EMPTY,PRD_ID_PREFIX_1,PRD_ID_PREFIX_2,TRIAL_ENTITY_NAME,TRIAL_ENTITY_LEI,TRIAL_REGION) 
              values (:RUN_DATE,:ACODE,:GMI_ACCOUNT,:GMI_OFFICE,:GMI_FIRM,:ACCOUNT_NAME,:CCY,:LE_ID,:MARKET,:GMF,:MIC_CODE,:GMI_PRD,:PRD,:PRD_DESCRIPTION,:BBG_BACK_OFFICE_TICKER,:BBG_FRONT_OFFICE_TICKER,:BBG_YELLOW_KEY,:ROOT_RIC,:RIC,:TRADE_TYPE,:PROMPT_DATE,:EXPIRY_DECLARATION,:LAST_TRADED_DATE,:STRIKE_PRICE,:OPTION_TYPE,:DELIVERY_TYPE,:TRADE_PRICE,:DECIMAL_TRADE_PRICE,:TRIAL_TRADEPRICE,:CONTRACTS,:NOTIONAL_FOR_FUTURES,:CLEARING_BROKER,:EXEC_BROKER,:TRADE_DATE,:TRANSACTION_TYPE,:UNDERLYING,:UNDERLYING_TYPE,:GMI_MULTIPLIER,:UTI,:USI,:ISIN,:AII,:CFI,:SERIAL,:DEALER_REFERENCE,:TRADE_EXECUTION_ID,:CLEARING_TIMESTAMP,:EXECUTION_TIMESTAMP,:CCP_LE_ID,:SWAP_TYPE,:EFFECTIVE_DATE,:COUPON_RATE,:DAY_COUNT_BASIS,:ROLL_FREQUENCY,:RESET_FREQUENCY,:ACTIVITY_TYPE,:EMPTY,:PRD_ID_PREFIX_1,:PRD_ID_PREFIX_2,:TRIAL_ENTITY_NAME,:TRIAL_ENTITY_LEI,:TRIAL_REGION)
              ]]&gt;
              &lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="itemSqlParameterSourceProvider"&gt;
              &lt;bean
                  class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" /&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>It took 118 seconds for 2,00,000 records.</strong></p>
  
  <p><strong>Approach 2 - BufferedReader &amp; JDBC Batch</strong> </p>
  
  <pre><code>Connection connection = dbManager.getConnection();
      File file = new File(filePath);
      BufferedReader br = null;
      boolean error = false;
      ArrayList&lt;String[]&gt; records = new ArrayList&lt;String[]&gt;(batchSize * 2);
      try {
          connection.setAutoCommit(false);
          br = new BufferedReader(new FileReader(file));
          String line;
          while ((line = br.readLine()) != null) {
  //      System.out.println(line);
          String[] values = line.split(",",-1);
          records.add(values);
          if (records.size() == batchSize) {
              insertToDB(records, connection);
              records.clear();
          }
          }
  
          if (records.size() &gt; 0) {
          insertToDB(records, connection);
          }
  
      } catch (FileNotFoundException e) {
          error = true;
          e.printStackTrace();
      } catch (IOException e) {
          error = true;
          e.printStackTrace();
      } catch (SQLException e) {
          error = true;
          e.printStackTrace();
          DBManager.rollback(connection);
      } finally {
          if (br != null)
          try {
              br.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
          if (!error) {
          DBManager.commitConnection(connection);
          }
  
          if (DBManager.isConnectionClosed(connection))
          DBManager.closeConnection(connection);
  
      }
  </code></pre>
  
  <p>It took 3 seconds for 1,00,000 records.</p>
  
  <p>Why Spring Batch Version 3 is slow ?? I had vice versa impression.</p>