What is the problem with this code?
I have a for loop like this:
PreparedStatement pstmt = null;
ResultSet rs1 =null;
//some code
for(int i = 0;i<someValue;i++)
{
try
{
//some code
rs = pstmt.executeQuery();
//some code that uses rs
}
catch(Exception e{}
//exit point
}
Now if I am not closing the resultset rs at exit point ,what is the bad thing about this code-performance issue/erroneous output or some thing else?
Thanks....>
[507 byte] By [
omijoa] at [2007-11-27 6:42:27]

Sorry I forgot to add a query!!!
Suppose the following scenario exists:
for (.......)
{
stmt = con.prepareStatement(query);
rs = stmt.executeQuery();
//some code
}
Now can a performance issue arise if the method containing this loop is called frequently?
omijoa at 2007-7-12 18:12:30 >

If you don't close the ResultSet, different things happen based on which database you're running. You can keep read locks that you don't need. It wastes resources, and as different databases react differently to it, you might have weird bugs down the road that result from your practices of not properly closing ResultSets, PreparedStatements, and Connections. Put this in a loop like you've got here, and the effects may or may not get amplified.
I write a close() for a ResultSet just as soon as I open it, just to make sure I don't forget. In a situation like this one, I would write something like so:
for(int i = 0;i<someValue;i++)
{
try
{
//some code
rs = pstmt.executeQuery();
//some code that uses rs
}
catch(Exception e)
{
}
finally
{
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
}
>
worse than that, database resources are scarse. If you don't return them you'll eventually run out, causing the database server to hang or crash.In a real environment that can cost LOTS of money, think millions.