Hi!
Question one:
I have bunches of the following code (there is Oracle 9i behind it):
public static Vector getSomeVector(Connection conn) throws
SQLException {
java.util.Vector result = new Vector();
java.sql.PreparedStatement pstmt = conn.prepareStatement(SOME_SQL);
try {
//Should I put this out of the try finally block. Start of block
pstmt.setInt(1, 123456);
pstmt.setString(2, "dummy");
java.sql.ResultSet rset = pstmt.executeQuery();
//End of block
while (rset.next()) {
result.add(rset.getString("SOME_FIELD"));
}
} finally {
pstmt.close();
}
}
I don't want to catch any exception, just want to close the
PreparedStatement the right way. The question is: Do I have to close
the _prepared_ statement or whether I have to close the _executed_
statement? I know it would probably work the both way, I just want to
clearify my idea of PreparedStatements.
Question Two:
I wonder if it is a good idea not to close after every execution, but
just once at the end of the loop? I have heard of caching at the
server side, and that the code below should have better performance if
the PreparedStatement is not closed after every execution. Is this
true?
public static Vector getSomeVector(Connection conn) throws
SQLException {
java.util.Vector result = new Vector();
java.sql.PreparedStatement pstmt = conn.prepareStatement(SOME_SQL);
try {
//Is it a good idea not to close after every execution, but just
once?
for(int i = 0; i < 10; i++) {
pstmt.setInt(1, 123456);
pstmt.setString(2, "dummy");
java.sql.ResultSet rset = pstmt.executeQuery();
while (rset.next()) {
result.add(rset.getString("SOME_FIELD"));
}
}
} finally {
pstmt.close();
}
}
Thanks in advance,
jeges
Joe Weinstein - 01 Nov 2004 16:42 GMT
Hi. Both code examples are fine as they are, with the second example
much better. If you know you're going to use a prepared statement many
times, use the same prepared statement.
Joe Weinstein at BEA
> Hi!
>
[quoted text clipped - 61 lines]
>
> jeges
MishGun - 02 Nov 2004 06:02 GMT
> } finally {
> pstmt.close();
[quoted text clipped - 3 lines]
> I don't want to catch any exception, just want to close the
> PreparedStatement the right way.
Hello,
you can write:
finally {
try {
pstmt.close();
} catch ( SQLException e ) { }
}
Jeges Endre - 04 Nov 2004 01:56 GMT
> > } finally {
> pstmt.close();
[quoted text clipped - 11 lines]
> } catch ( SQLException e ) { }
> }
Hello!
Thanks for the answers. Yes I added it already, because of the second
throwed exception overrides the first.
Ergo: I have to close statements because they are prepared, and not
because they are executed.
jeges
Daniel Dittmar - 04 Nov 2004 13:49 GMT
> Ergo: I have to close statements because they are prepared, and not
> because they are executed.
If the execute creates a ResultSet, then the ResultSet should be closed
as well. Although most drivers will close the ResultSet a) when the
PreparedStatement is executed again and b) when the PreparedStatement is
closed.
Daniel
Robert Sundström - 05 Nov 2004 10:45 GMT
>> Ergo: I have to close statements because they are prepared, and not
>> because they are executed.
[quoted text clipped - 3 lines]
> PreparedStatement is executed again and b) when the PreparedStatement is
> closed.
There might be some driver here and there which does not fully comply with
the JDBC specification and leave ResultSet objects open. However, the JDBC
specification explicitly says that ResultSet objects are to be closed by
the driver when the corresponding
Statement/PreparedStatement/CallableStatement is closed or reexecuted. So,
a JDBC compliant application is not required to close ResultSet objects,
unless it wishes to free resources held by the ResultSet object before the
Statement/PreparedStatement/CallableStatement is close or executed again.

Signature
Robert Sundström, Mimer SQL Development
Mimer Information Technology AB, http://www.mimer.com
Validate your SQL statements/procedures at
http://developer.mimer.com/validator
Daniel Dittmar - 05 Nov 2004 11:27 GMT
> There might be some driver here and there which does not fully comply
> with the JDBC specification and leave ResultSet objects open. However,
[quoted text clipped - 5 lines]
> before the Statement/PreparedStatement/CallableStatement is close or
> executed again.
Yes.
But if you're using a connection pool (common) and attach
PreparedStatements to sessions to pool them as well (rare), then it is
highly advisable to close ResultSets before you return the session back
into the pool.
Daniel