BAEL-1348 | BatchProcessing example using jdbc (#3216)

* BAEL-EVAL | Adding an example to demo Bean Injection

### Changes done
A Spring boot app to demo bean injection

* Adding @Autowired for Constructor Injection

* BAEL-EVAL | Correcting the method name

* BAEL-EVAL

* BAEL-1348 | Adding example of Batch Processing in JDBC

### Changes done
- Added one example using statement
- Added one example using preparedstatement

* Removing evaluation article

* BAEL-1348 | Adding example of Batch Processing in JDBC

### Changes done
- Added an example using statement
- Added an example using preparedstatement

* BAEL-1348 | BatchProcessing example using jdbc

### Changes done
- Add example using statement
- Add example using PreparedStatement
- Add Unit Tests

* BAEL-1348 | Changes to pom.xml and refactoring UnitTest

* BAEL-1348 | Removing the example using spring-boot
This commit is contained in:
Binesh Narayanan 2017-12-19 14:37:35 +08:00 committed by Grzegorz Piwowarek
parent 617c2ffdba
commit 94ace2eda8
3 changed files with 612 additions and 446 deletions

View File

@ -180,12 +180,7 @@
<artifactId>esapi</artifactId> <artifactId>esapi</artifactId>
<version>2.1.0.1</version> <version>2.1.0.1</version>
</dependency> </dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.196</version>
<scope>runtime</scope>
</dependency>
<dependency> <dependency>
<groupId>com.sun.messaging.mq</groupId> <groupId>com.sun.messaging.mq</groupId>
<artifactId>fscontext</artifactId> <artifactId>fscontext</artifactId>
@ -221,13 +216,17 @@
<artifactId>spring-web</artifactId> <artifactId>spring-web</artifactId>
<version>4.3.4.RELEASE</version> <version>4.3.4.RELEASE</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId> <artifactId>spring-boot-starter</artifactId>
<version>1.5.8.RELEASE</version> <version>1.5.8.RELEASE</version>
</dependency> </dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.4.0</version>
<scope>runtime</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
@ -261,7 +260,7 @@
<exclude>**/*LongRunningUnitTest.java</exclude> <exclude>**/*LongRunningUnitTest.java</exclude>
<exclude>**/*ManualTest.java</exclude> <exclude>**/*ManualTest.java</exclude>
</excludes> </excludes>
<testFailureIgnore>true</testFailureIgnore>
</configuration> </configuration>
</plugin> </plugin>
@ -385,7 +384,7 @@
<argument>-Xmx300m</argument> <argument>-Xmx300m</argument>
<argument>-XX:+UseParallelGC</argument> <argument>-XX:+UseParallelGC</argument>
<argument>-classpath</argument> <argument>-classpath</argument>
<classpath /> <classpath/>
<argument>com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed</argument> <argument>com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed</argument>
</arguments> </arguments>
</configuration> </configuration>
@ -433,7 +432,7 @@
<executions> <executions>
<execution> <execution>
<id>run-benchmarks</id> <id>run-benchmarks</id>
<!-- <phase>integration-test</phase> --> <!-- <phase>integration-test</phase>-->
<phase>none</phase> <phase>none</phase>
<goals> <goals>
<goal>exec</goal> <goal>exec</goal>
@ -443,7 +442,7 @@
<executable>java</executable> <executable>java</executable>
<arguments> <arguments>
<argument>-classpath</argument> <argument>-classpath</argument>
<classpath /> <classpath/>
<argument>org.openjdk.jmh.Main</argument> <argument>org.openjdk.jmh.Main</argument>
<argument>.*</argument> <argument>.*</argument>
</arguments> </arguments>

View File

@ -0,0 +1,96 @@
package com.baeldung.jdbc;
import java.sql.*;
import java.util.UUID;
public class BatchProcessing {
private final String[] EMPLOYEES = new String[]{"Zuck","Mike","Larry","Musk","Steve"};
private final String[] DESIGNATIONS = new String[]{"CFO","CSO","CTO","CEO","CMO"};
private final String[] ADDRESSES = new String[]{"China","York","Diego","Carolina","India"};
private Connection connection;
public void getConnection(){
try {
Class.forName("org.hsqldb.jdbcDriver");
connection = DriverManager.getConnection("jdbc:hsqldb:file:C:\\EMPLOYEEDB", "SA", "");
connection.setAutoCommit(false);
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
public void createTables(){
try {
connection.createStatement().executeUpdate("create table EMPLOYEE (ID VARCHAR(36), NAME VARCHAR(45), DESIGNATION VARCHAR(15))");
connection.createStatement().executeUpdate("create table EMP_ADDRESS (ID VARCHAR(36), EMP_ID VARCHAR(36), ADDRESS VARCHAR(45))");
System.out.println("Tables Created!!!");
} catch (SQLException e) {
e.printStackTrace(System.out);
}
}
public void useStatement(){
try {
String insertEmployeeSQL = "INSERT INTO EMPLOYEE(ID, NAME, DESIGNATION) VALUES ('%s','%s','%s');";
String insertEmployeeAddrSQL = "INSERT INTO EMP_ADDRESS(ID, EMP_ID, ADDRESS) VALUES ('%s','%s','%s');";
Statement statement = connection.createStatement();
for(int i = 0; i < EMPLOYEES.length; i++){
String employeeId = UUID.randomUUID().toString();
statement.addBatch(String.format(insertEmployeeSQL, employeeId, EMPLOYEES[i],DESIGNATIONS[i]));
statement.addBatch(String.format(insertEmployeeAddrSQL, UUID.randomUUID().toString(),employeeId,ADDRESSES[i]));
}
statement.executeBatch();
connection.commit();
} catch (Exception e) {
try {
connection.rollback();
} catch (SQLException ex) {
System.out.println("Error during rollback");
System.out.println(ex.getMessage());
}
e.printStackTrace(System.out);
}
}
public void usePreparedStatement(){
try {
String insertEmployeeSQL = "INSERT INTO EMPLOYEE(ID, NAME, DESIGNATION) VALUES (?,?,?);";
String insertEmployeeAddrSQL = "INSERT INTO EMP_ADDRESS(ID, EMP_ID, ADDRESS) VALUES (?,?,?);";
PreparedStatement employeeStmt = connection.prepareStatement(insertEmployeeSQL);
PreparedStatement empAddressStmt = connection.prepareStatement(insertEmployeeAddrSQL);
for(int i = 0; i < EMPLOYEES.length; i++){
String employeeId = UUID.randomUUID().toString();
employeeStmt.setString(1,employeeId);
employeeStmt.setString(2,EMPLOYEES[i]);
employeeStmt.setString(3,DESIGNATIONS[i]);
employeeStmt.addBatch();
empAddressStmt.setString(1,UUID.randomUUID().toString());
empAddressStmt.setString(2,employeeId);
empAddressStmt.setString(3,ADDRESSES[i]);
empAddressStmt.addBatch();
}
employeeStmt.executeBatch();
empAddressStmt.executeBatch();
connection.commit();
} catch (Exception e) {
try {
connection.rollback();
} catch (SQLException ex) {
System.out.println("Error during rollback");
System.out.println(ex.getMessage());
}
e.printStackTrace(System.out);
}
}
public static void main(String[] args) {
BatchProcessing batchProcessing = new BatchProcessing();
batchProcessing.getConnection();
batchProcessing.createTables();
batchProcessing.useStatement();
batchProcessing.usePreparedStatement();
}
}

View File

@ -0,0 +1,71 @@
package com.baeldung.jdbc;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
@RunWith(MockitoJUnitRunner.class)
public class BatchProcessingTest {
@InjectMocks
private BatchProcessing target = new BatchProcessing();
@Mock
private Connection connection;
@Mock
private Statement statement;
@Mock
private PreparedStatement employeeStatement;
@Mock
private PreparedStatement employeeAddressStatement;
@Before
public void before(){
MockitoAnnotations.initMocks(this);
}
@Test
public void when_useStatement_thenInsertData_success() throws Exception {
Mockito.when(connection.createStatement()).thenReturn(statement);
target.useStatement();
}
@Test
public void when_useStatement_ifThrowException_thenCatchException() throws Exception {
Mockito.when(connection.createStatement()).thenThrow(new RuntimeException());
target.useStatement();
}
@Test
public void when_usePreparedStatement_thenInsertData_success() throws Exception {
String insertEmployeeSQL = "INSERT INTO EMPLOYEE(ID, NAME, DESIGNATION) VALUES (?,?,?);";
String insertEmployeeAddrSQL = "INSERT INTO EMP_ADDRESS(ID, EMP_ID, ADDRESS) VALUES (?,?,?);";
Mockito.when(connection.prepareStatement(insertEmployeeSQL)).thenReturn(employeeStatement);
Mockito.when(connection.prepareStatement(insertEmployeeAddrSQL)).thenReturn(employeeAddressStatement);
target.usePreparedStatement();
}
@Test
public void when_usePreparedStatement_ifThrowException_thenCatchException() throws Exception {
String insertEmployeeSQL = "INSERT INTO EMPLOYEE(ID, NAME, DESIGNATION) VALUES (?,?,?);";
String insertEmployeeAddrSQL = "INSERT INTO EMP_ADDRESS(ID, EMP_ID, ADDRESS) VALUES (?,?,?);";
Mockito.when(connection.prepareStatement(insertEmployeeSQL)).thenReturn(employeeStatement);
Mockito.when(connection.prepareStatement(insertEmployeeAddrSQL)).thenThrow(new RuntimeException());
target.usePreparedStatement();
}
}