Search This Blog

Thursday 11 October 2007

PreparedStatement method setNClob(int, Reader) causes HANG with 11g JDBC Driver

When using JDK 1.6 with the new 11G JDBC driver the following code causes a HANG to occur.

// Get connection
Connection conn = getConnection();

// start test
String lobContent = "WLS JDBC4 test for NClob.";

String insertSql = "INSERT INTO nclob_table VALUES (?, ?)";
PreparedStatement pstmt = conn.prepareStatement(insertSql);

int loopCount = 10;
StringReader sreader = null;

try
{
for (int i = 0; i < loopCount; i++)
{
pstmt.setInt(1, i);
sreader = new StringReader(lobContent + "-" + i);
// Hangs here
pstmt.setNClob(2, sreader);
pstmt.execute();
sreader.close();
System.out.println("inserted a row..." );
}
}
finally
{
if (pstmt != null)
{
pstmt.close();
}
if (conn != null)
{
conn.close();
}
}
}


The hang occurs at this line of code:

pstmt.setNClob(2, sreader);

In order to avoid the hang you can use the following method:
  • setNClob(int, Reader, long)

This then works fine when the following is used.

pstmt.setNClob(2, sreader, sreader.toString().length());

No comments: