Monday, June 19, 2006

Java Object to Oracle Object mapping

I had a requirement of passing large data to Oracle db and the various options I evaluated are:

1. Using PreparedStatements and batching the updates/inserts: Though this option seems apparent, I didn't consider it as a candidate for the reason that we need to use raw SQL statements and this violates our DB convention to use stored procs and functions. I'm not very sure if this would be fast either. So, dropped this option.

2. Using CLOBs: This would require a lot of change in the existing procs, so dropped it for the sake of reusing the existing procs. Someone, told me that CLOBs do not scale well.

3. Using Arrays: This is an elegant option. Most of the time, the data sent in bulk to DBs is primitive, ie, some numbers or strings or atmost encoded strings (NVARCHAR2). To evaluate sending info using array, I did the following:
a) created a table with just two columns first_name and last_name
b) created a oracle type to be a table of varchar2
create or replace type str_typ_t as table of varchar2(50);
c) created a stored proc to accept the string array as input and insert into the table.
d) The java class required some googling and experimentation, here is the code snippet:

ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor("STR_TYP_T",conn);
ARRAY fnArray = new ARRAY(descriptor,conn,fnames);

That is all to be done. And I'm very happy to note the results. 300 inserts took just over a second!!!. This is Tres cool.

And if you would like to pass a java object to the stored proc and leave the mapping to the driver, the following lines would do the work:

StructDescriptor descriptor = StructDescriptor.createDescriptor("TEST_OBJ_T",conn);
Object[] attributes = {"ABCD","XYZ"};
STRUCT to = new STRUCT(descriptor,conn,attributes);


Some progress today. My PL/SQL skills have been really rusty, need more practice.

No comments: