Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple ResultSets

Status
Not open for further replies.

evergrean100

Technical User
Dec 1, 2006
115
US
Is it okay to create 3 different ResultSets with the same Statement object? Here is what I am currently using in my Database statements with Oracle and everything works great. But I am wondering if this will create Database resource leakages or other issues:

Code:
//Db connection called from another Java class
.....
ResultSet results1 = null;
ResultSet results2 = null;
ResultSet results3 = null;
Statement statement = null;
......
        statement = connection.createStatement();
        results1 = statement.executeQuery("select name from tableone");
        if(results1.next())
        {
             int myvar = ....
        }
 
        results2 = statement.executeQuery("select food, sugar from tabletwo");
        if(results2.next())
        {
             int myvar2 = ....
        }
 
        results3 = statement.executeQuery("select rock from tablethree");
        if(results3.next())
        {
             int myvar3 = ....
        }
 
        ...
        //Finally block here that closes results1, results2, results3, statement and connection object references
 
Thanks, it seems to be okay but in my Tomcat Startup Dos window it keeps printing out my Catch Resultset output saying "closing resultset error" so I eliminated all the close the resultsets and that eliminated the "closing resultset error". I kept the statement and connection close statements.

Will this cause a database resource leak issue even though I still will have the statement and connection close methods in the Finally block?
 
from Javadocs, ResultSet:

Note: A ResultSet object is automatically closed by the Statement object that generated it when that Statement object is closed, re-executed, or is used to retrieve the next result from a sequence of multiple results.

and:
Calling the method close on a ResultSet object that is already closed is a no-op.

javadoc, Statement:
Calling the method close on a Statement object that is already closed has no effect.
Note: When a Statement object is closed, its current ResultSet object, if one exists, is also closed.

don't visit my homepage:
 
i would prefer to have a pair of statement and resultset for each SQL statement.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top