Monday, July 10, 2006

CLOB issues in weblogic

I noticed a strange behavior with CLOB in weblogic. The CLOBs would work fine with the main method, but when the same code is run on Weblogic, it throws an "No more data to read from socket" exception. The interesting aspect is, the row in the table gets inserted perfectly including the CLOB column.

The first thing I suspected is with the driver version of the ojdbc.jar shipped with WLS, but that wasn't the problem as the driver I used in the application is same as the one shipped with WLS.

Since, the infrastructure appeared fine, I decided to debug and track the root cause. Debugging the problem brought out some intriguing points. The hack is to create the CLOB column with the default as EMPTY_CLOB() and do a post process after the insertion. The sequence would be:

1. insert some dummy string first cs.setString(4," ");
2. Excecute the proc
3. post process the clob

Clob clob = null;
pstmt = conn.prepareStatement("SELECT clob_col from clob_tbl where id=? FOR UPDATE");
pstmt.setString(1,id);
rs = pstmt.executeQuery();
if(rs.next()) {
clob = rs.getClob(1);
Writer writer = ((OracleThinClob)clob).getCharacterOutputStream();
writer.write(msg);
writer.close();
}
st = conn.prepareStatement("UPDATE clob_tbl set clob_col=? where id=?");
st.setClob(1,clob);
st.setString(2,msgId);
st.executeUpdate();


That is all to be done. Though, there is an additional step in the flow of control, this solution is clean. Whew! some solace.

No comments: