Java Clobs to String Hangs (most the time)
I am using Java 1.6.22, JBoss 4.2.3, Oracle 10.2.0.5, OCI DB connection, and have tried both JDBC14 and latest JDBC6.
I am having issues converting a Clob to a string without getting a hung status, and I am looking for some help because my co-workers and I cannot figure out a solution.
I first started using result.getString(columnName), but I getting my data hung on this line. So I switched to result.getClob and then result.getObject, and I am able to get the results back quickly, but I cannot convert the clob to a string without getting a hung state.
The clob has to be converted to string so I can display it to my web page.
I am able to convert about 25% of my clobs to a string, but the other 75% hang always on this line:
while ((c = read.read()) != -1) {
or this line (depending on how I write the code)
while ((c = read.readline()) != -1) {
or this line (depending on how I write the code)
IOUtils.copy(in, w);
or this line (depending on how I write the code)
JavaClobContent.getSubString(1, clobLength);
or this line (depending on how I write the code)
result.getString(columnName)
Also, I have tried using the latest Apache commons-io-2.0:
Reader in = new BufferedReader(JavaClobContent.getCharacterStream());
StringWriter w = new StringWriter();
// Hangs on the line below
IOUtils.copy(in, w);
return w.toString();
I even tried to chunk the data into a string, but it seams to hang on getSubString.
stringContentOutput = JavaClobContent.getSubString(1, 1000);
stringContentOutput += JavaClobContent.getSubString(1001, 2000);
Below is the code snippets of the latest process that I have tried, but it is hanging:
Statement statement = connection.createStatement();
String sql = null;
String clob_column = null;
sql = "SELECT clob_column from clob_table where ID = 1";
ResultSet result = statement.executeQuery(sql);
if(result.next()) {
clob_column = getStringFromField(result, "clob_column");
}
String getStringFromField(ResultSet result, String columnName) {
String value = "";
Object fieldValue = "NULL";
try {
if ((null == columnName) || (columnName.equals(""))) return (value);
if (null != result) {
fieldValue = result.getObject(columnName);
if (null != fieldValue) {
if (fieldValue instanceof Clob) {
try {
StringWriter write = new StringWriter();
InputStream in = ((Clob) fieldValue).getAsciiStream();
Reader read = new InputStreamReader(in);
int c = -1;
while ((c = read.read()) != -1) {
write.write(c);
}
write.flush();
value = write.toString();
} catch (Exception e) {
log.error("Error found: ", e)
}
}
}
} catch (Exception e) {
log.error("Exception occurred getting object from resultSet. columnName = " + columnName, e);
value = "NULL";
}
value = cleanupClob(value);
return (value);
}
Any ideas on what I am doing incorrectly.