Reputation: 441
I'm trying to understand the implications of executing a statement on a connection while processing the results of a different statement execution on the same connection.
Take this example:
public void doSomething(){
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement("some sql");
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
//...do some stuff with the results
/*
pass the db connection so we don't create
too many connections to the database
*/
doSomethingElse(dbConnection);
}
} catch (SQLException e) {
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
public void doSomethingElse(Connection dbConnection){
PreparedStatement preparedStatement2 = null;
try {
//using the same connection from "doSomething"
preparedStatement2 = dbConnection.prepareStatement("some more sql");
ResultSet rs2 = preparedStatement2.executeQuery();
while (rs2.next()) {
//...do some stuff with the results
}
} catch (SQLException e) {
} finally {
if (preparedStatement2 != null) {
preparedStatement2.close();
}
}
}
My main question is this: What is the behavior of executing a statement on a connection before closing the statement using the same connection? Does either of the results get corrupted by executing another statement on the same connection? I know it's a no-no to do something like this, but what is the exact behavior? Or is the behavior unpredictable?
I've been looking but haven't been able to find an answer or an example of this anywhere. Any insight or direction would be great.
Upvotes: 1
Views: 33
Reputation: 33010
The JDBC 3.0 spec states:
13.1.1 Creating Statements
... Each Connection object can create multiple Statement objects that may be used concurrently by the program...
Upvotes: 2