Craig L Russell wrote:
> Long term, I don't believe that Derby or any other implementation
> should try to optimize for applications written without regard for good
> programming patterns.
In this case Derby needs to follow the JDBC spec and correctly handle
garbage collected JDBC objects. I don't think anyone is optimising Derby
here, just ensure it behaves correctly. Derby should not fall over
because of a badly written application, that will not encourage people
to use Derby. Fact of life is that people write bad JDBC applications,
Derby needs to handle those.
Look at this code, I would say this is good, clean valid and common JDBC
programming:
long someMethod(Connection conn, ...)
throws SQLException
{
PreparedStatement ps = conn.prepareStatement(MY_SQL);
ps.setInt(1, ...);
ps.setInt(1, ...);
ResultSet rs = ps.executeQuery();
// use rs to calulate result
rs.close();
ps.close();
return result;
}
Note that the result set and prepared statement will not be closed on an
exception, but they will be garage collected. If you require the
application must explicitly close every JDBC object to use Derby, then
the code becomes something ugly like:
long someMethod(Connection conn, ...)
throws SQLException
{
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(MY_SQL);
ps.setInt(1, ...);
ps.setInt(1, ...);
rs = ps.executeQuery();
// use rs to calulate result
rs.close();
ps.close();
return result;
} catch (SQLException sqle)
{
try {
if (rs != null)
rs.close();
} catch (SQLException sqle2)
{
sqle.setNextException(sqle2);
}
try {
if (ps != null)
ps.close();
} catch (SQLException sqle3)
{
sqle.setNextException(sqle3);
}
throw sqle;
}
}
> That said, OutOfMemoryError is unfortunate, but perhaps unavoidable.
> Does the test succeed, given enough memory? Does closing the result
> sets and prepared statements change the behavior? How can Derby know
> whether you intend to use the result set and prepared statement again,
> and you actually want to keep them open? Do you want Derby to
> internally close result sets and prepared statements that it guesses
> you no longer want? In a large system, wouldn't it be a bug in Derby if
> Derby closed result sets and prepared statements that the application
> still wanted?
There's no guessing, if the application has a reference to an open JDBC
object then it is still using it. If it no longer has a reference to a
open JDBC open then it is no longer using it and Derby must garbage
collect it.
Dan.