FileDocCategorySizeDatePackage
TestStandardBatching.javaAPI DocExample6774Mon Jul 02 20:31:54 BST 2001None

TestStandardBatching

public class TestStandardBatching extends Object

Fields Summary
Connection
conn
Constructors Summary
public TestStandardBatching()

    try {
      DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
      conn = DriverManager.getConnection(
       "jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");
    }
    catch (SQLException e) {
      System.err.println(e.getMessage());
      e.printStackTrace();
    }
  
Methods Summary
protected voidfinalize()

    if (conn != null) 
      try { conn.close(); } catch (SQLException ignore) { }
    super.finalize();
  
public static voidmain(java.lang.String[] args)

    new TestStandardBatching().process(args[0]);
  
public voidprocess(java.lang.String iterations)

    int               rows    = 0;
    int               last    = new Integer(iterations).intValue();
    long              start   = 0;
    long              end     = 0;
    Statement         stmt    = null;
    PreparedStatement pstmt   = null;
    String            text    = 
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890" +
     "12345678901234567890123456789012345678901234567890";

    // Set autocommit off
    try {
      conn.setAutoCommit(false);
      conn.commit();
    } 
    catch (SQLException e) {
      System.err.println(e.getMessage());
    }

    // One statement at a time, awfully slow!
    try {
      start = System.currentTimeMillis();
      stmt = conn.createStatement();
      for (int i=0;i < last;i++) {
        rows = stmt.executeUpdate(
         "insert into test_batch " +
         "( test_batch_id, text ) " + 
         "values " + 
         "( test_batch_id.nextval, '" + text + "' )");
      }
      end = System.currentTimeMillis();
      stmt.close();
      stmt = null;
      conn.commit();
      System.out.println(
       last + " inserts using statement:                   " + 
       (end - start) + " milliseconds");
    }
    catch (SQLException e) {
      System.err.println(e.getMessage());
    }
    finally {
      if (stmt != null) 
        try { stmt.close(); } catch (SQLException ignore) { }
    }

    // One prepared statement at a time, better!
    try {
      pstmt = conn.prepareStatement(
       "insert into test_batch " +
       "( test_batch_id, text ) " + 
       "values " + 
       "( test_batch_id.nextval, ? )");
      start = System.currentTimeMillis();
      for (int i=0;i < last;i++) {
        pstmt.setString( 1, text );
        rows = pstmt.executeUpdate();
      }
      end = System.currentTimeMillis();
      pstmt.close();
      pstmt = null;
      conn.commit();
      System.out.println(
       last + " inserts using prepared statement:          " + 
       (end - start) + " milliseconds");
    }
    catch (SQLException e) {
      System.err.println(e.getMessage());
    }
    finally {
      if (pstmt != null) 
        try { pstmt.close(); } catch (SQLException ignore) { }
    }

    // But now using standard batching: wow!!!
    try {
      pstmt = conn.prepareStatement(
       "insert into test_batch " +
       "( test_batch_id, text ) " + 
       "values " + 
       "( test_batch_id.nextval, ? )");
      start = System.currentTimeMillis();
      for (int i=0;i < last;i++) {
        pstmt.setString( 1, text );
        pstmt.addBatch();
      }
      int [] rowArray = pstmt.executeBatch();  
      end = System.currentTimeMillis();
      pstmt.close();
      pstmt = null;
      conn.commit();
      System.out.println(
       last + " inserts using prepared statement batching: " + 
       (end - start) + " milliseconds");
    }
    catch (SQLException e) {
      System.err.println(e.getMessage());
    }
    finally {
      if (pstmt != null) 
        try { pstmt.close(); } catch (SQLException ignore) { }
    }

    // Clean up
    try {
      stmt = conn.createStatement();
      rows = stmt.executeUpdate("delete test_batch");
      conn.commit();
      stmt.close();
      stmt = null;
      System.out.println(rows + " rows deleted");
      System.out.println("");
    }
    catch (SQLException e) {
      System.err.println(e.getMessage());
    }
    finally {
      if (stmt != null) 
        try { stmt.close(); } catch (SQLException ignore) { }
    }