< Prev - Examples - Next >

Bulk Processing - Inserting Rows Using PL/SQL Tables

Example Description

Table : BULK_PROCESSING_TABLE
create table bulk_processing_table
(
n    number not null,
d    date not null,
s    varchar2(100) not null
);
PL/SQL Package Specification : BULK_PLSQL_TABLE
create or replace package bulk_plsql_table
as

type plsql_table_number is table of number index by pls_integer;
type plsql_table_date is table of varchar2(8) index by pls_integer;
type plsql_table_varchar is table of varchar2(100) index by pls_integer;

procedure bulk_plsql_table
(
  i_plsqltab_number   in plsql_table_number, 
  i_plsqltab_date     in plsql_table_date, 
  i_plsqltab_varchar  in plsql_table_varchar
);

end bulk_plsql_table;
PL/SQL Package Body : BULK_PLSQL_TABLE
create or replace package body bulk_plsql_table
as

procedure bulk_plsql_table
(
  i_plsqltab_number   in plsql_table_number, 
  i_plsqltab_date     in plsql_table_date, 
  i_plsqltab_varchar  in plsql_table_varchar
)
is
begin
  forall i in 1..i_plsqltab_number.count
    insert into bulk_processing_table
      (n,d,s)
      values
      (i_plsqltab_number(i), to_date(i_plsqltab_date(i), 'yyyy/mm/dd'), i_plsqltab_varchar(i));
end bulk_plsql_table;

end bulk_plsql_table;
Java Calling Class : BulkCollectionPlSqlTable.java
package plsql_workbench_examples;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.UUID;

import factory.ExamplesRPCFactory;
import service.BulkPlsqlTableService;

public class BulkCollectionPlSqlTable {
  private final static int ELEMENTS = 50000;

  public static void main(String[] args) {
    try {
      // getting the service
      BulkPlsqlTableService service = ExamplesRPCFactory.getBulkPlsqlTableService();

      // date is transmitted as varchar2 and converted in pl/sql as date type again, because only number and varchar2 types are 
      // supported thru jdbc driver
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

      // generating 50000 elements to transfer to the stored procedure 
      Integer[] numberList = new Integer[ELEMENTS];
      String[] dateList = new String[ELEMENTS];
      String[] stringList = new String[ELEMENTS];
      for (int i = 0; i < ELEMENTS; i++) {
        numberList[i] = (int) (Math.random() * Integer.MAX_VALUE);
        dateList[i] = sdf.format(new Date(System.currentTimeMillis() + (long) (Math.random() * Integer.MAX_VALUE)));
        stringList[i] = UUID.randomUUID().toString();
      }
      
      // timer to check the throughput
      TimerCounter tc = new TimerCounter();

      // calling the stored procedure
      service.bulkPlsqlTable(numberList, dateList, stringList);

      // print out throughput
      System.out.println(tc.perSecond("pl/sql bulk performance", ELEMENTS));
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}