Tuesday 5 June 2012

Batch Processing

If a JDBC application contains multiple DML queries, to execute these queries our jdbc application requires n number of network round trips between jdbc application and database application which will reduces the performance

In order to execute multiple DML Queries in a single request, we must add multiple DML queries as a single unit. this single unit is known as Batch

Def: The Process of submitting the group of DML Statements as a single unit to the database is known as Batch Processing.
                                           or
The Process of executing group of DML statements at a time in the database is known as Batch Processing. 
Steps for performing Batch Processing:

1. set auto commit of our jdbc application as false
                    java.sql.Connection
                                 |
          public void setAutoCommit(boolean)
ex:con.setAutoCommit(false);

2.Prepare(or) group of DML Queries
                  java.sql.Statement
                          |
               public void addBatch(String)
String q1="insert into emp values(1,'abc','hyd')";
String q2="insert into emp values(2,'xyz','hyd')";
String q3="insert into emp values(3,'pqr','vizag')";
Statement st=con.createStatement();
st.addBatch(q1);
st.addBatch(q2);
st.addBatch(q3);

3.Submit Batch of DML statements for executing in the database
                           java.sql.Statement
                                    |
                        public int[] executeBatch()
int s[]=st.executeBatch();
 To find no|| of rows effected after executing executeBatch()
int s[]=st.executeBatch();
int x=0;
for(int i=0;i<s.length;i++)
{
x=x+s[i];
}
System.out.println("no rows afftected="+x);

4.if all DML Statements in Batch will execute successfully then we can say commit.
                              java.sql.Connection
                                          |
                              public void commit()
                             public void rollback()
Note: In order to find the number of Batch Statements are successfully executing after generating BatchUpdateException we need to use the following method
                      java.sql.BatchUpdateException
                                        |
                     public int getUpdateCount();

Example Program to Describe Batch Processing

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import oracle.jdbc.OracleDriver;
public class class4 {
public static void main(String[] args) throws Exception {
oracle.jdbc.driver.OracleDriver obj=new OracleDriver();
 DriverManager.deregisterDriver(obj);
 Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "system123");
 Statement st=con.createStatement();
 String q1="insert into emp values(1,'abc',5000)";
 String q2="insert into emp values(2,'abc2',5000)";
 String q3="insert into emp values(3,'abc3',5000)";
 String q4="insert into emp values(4,'abc4',5000)";
 con.setAutoCommit(false);
 st.addBatch(q1);
 st.addBatch(q2);
 st.addBatch(q3);
 st.addBatch(q4);
 int res[]=st.executeBatch();
 con.commit();
 con.setAutoCommit(true);
 System.out.println("number of rows "+res.length);
}
}


      

No comments:

Post a Comment