Batch Processing

Batch Processing

In this tutorial, we will discuss batch processing in the spring framework using Spring DAO/JDBC. To perform Batch processing in Spring JDBC, we have to use the following method from JdbcTemplate class.

public int[] batchUpdate(String sql_prepared_Statement, BatchPreparedStatementSetter setter)

Where BatchPreparedStatementSetter interface contains the following two methods

public void setValues(PreparedStatement ps, int index)
public int getBatchSize()

Where setValues() method will execute for each record to set values to the positional parameters that existed in the PreparedStatement object by getting values from the provided list.

Batch Processing in Spring DAO

E.g

package com.ashok.spring.dao.batchprocessing.dto;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class Employee {
   private String empName;
   private String empId;
   private String empAddress;

   public String getEmpName() {
      return empName;
   }

   public void setEmpName(String empName) {
      this.empName = empName;
   }

   public String getEmpId() {
      return empId;
   }

   public void setEmpId(String empId) {
      this.empId = empId;
   }

   public String getEmpAddress() {
      return empAddress;
   }

   public void setEmpAddress(String empAddress) {
      this.empAddress = empAddress;
   }

   @Override
   public String toString() {
      return "Employee [empName=" + empName + ", empId=" + empId + ", empAddress=" + empAddress + "]";
   }
}
package com.ashok.spring.dao.batchprocessing;

import java.util.List;
import com.ashok.spring.dao.batchprocessing.dto.Employee;
/**
 * 
 * @author ashok.mariyala
 *
 */
public interface EmployeeDao {
   public int[] insert(List<Employee> list);
}
package com.ashok.spring.dao.batchprocessing;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;

import com.ashok.spring.dao.batchprocessing.dto.Employee;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class EmployeeDaoImpl implements EmployeeDao {
   JdbcTemplate jdbcTemplate;

   public void setDataSource(DataSource dataSource) {
      jdbcTemplate = new JdbcTemplate(dataSource);
   }

   @Override
   public int[] insert(List<Employee> list) {
      int[] rowCounts = null;
      try {
         String sql = "insert into employee values(?,?,?)";
         rowCounts = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
               ps.setString(1, list.get(i).getEmpId());
               ps.setString(2, list.get(i).getEmpName());
               ps.setString(3, list.get(i).getEmpAddress());
            }

            @Override
            public int getBatchSize() {
               return list.size();
            }
         });
      } catch (Exception e) {
         e.printStackTrace();
      }
      return rowCounts;
   }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
 <bean id="empDao" class="com.ashok.spring.dao.batchprocessing.EmployeeDaoImpl">
  <property name="dataSource" ref="dataSource" />
 </bean>
 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="${jdbc.driverClassName}" />
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.username}" />
  <property name="password" value="${jdbc.password}" />
 </bean>
 <context:property-placeholder
  location="com/ashok/spring/dao/batchprocessing/config/jdbc.properties" />
</beans>
jdbc.driverClassName = com.mysql.cj.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/employee
jdbc.username = root
jdbc.password = ashok
package com.ashok.spring.dao.batchprocessing;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ashok.spring.dao.batchprocessing.dto.Employee;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class TestSpringBatchProcessing {
   public static void main(String[] args) {
      String configFile = "/com/ashok/spring/dao/batchprocessing/config/applicationContext.xml";
      ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
      EmployeeDao dao = (EmployeeDao) context.getBean("empDao");
      List<Employee> list = new ArrayList<>();
      Employee e1 = new Employee();
      e1.setEmpId("Emp0087");
      e1.setEmpName("Ashok Kumar");
      e1.setEmpAddress("Bhimavaram");
      list.add(e1);
      Employee e2 = new Employee();
      e2.setEmpId("Emp65894");
      e2.setEmpName("Vinod Kumar");
      e2.setEmpAddress("Banglore");
      list.add(e2);
      Employee e3 = new Employee();
      e3.setEmpId("Emp85974");
      e3.setEmpName("Naresh Kumar");
      e3.setEmpAddress("Hyderabad");
      list.add(e3);
      int[] rowCounts = dao.insert(list);
      for (int i = 0; i < rowCounts.length; i++) {
         System.out.println(rowCounts[i]);
      }
   }
}

Output

1
1
1
Batch Processing

Scroll to top