Can ResultSet be reliably returned from method that creates Stmt and executes query?
Marshall B Thompson
Ranch Hand
Joined: Apr 11, 2002
Posts: 42
posted
0
method x creates a statement, executes a query, returns a resultset. In main, I create a resultset reference variable and set it by calling x. The thread below is a long argument about whether garbage collection of the statement object could potentially render my resultset to be null in main. See link: http://forum.java.sun.com/thread.jsp?forum=48&thread=88874 It appears that returning a resultset is a no-no, though logically, since the resultset has a reference to its statement object, it should be okay. Anyone know for sure?
Marshall B Thompson
Ranch Hand
Joined: Apr 11, 2002
Posts: 42
posted
0
I inherited some code that returns ResultSets from methods. When researching the proper way to clean up in my method, I ran across the referenced thread in one of Sun's forums. The thread seems to imply that returning a ResultSet from a method that created the Statement object is fatally flawed due to potential garbage collection of the Statement object. Has anyone observed this behavior?
Jason Steele
Ranch Hand
Joined: Apr 25, 2003
Posts: 100
posted
0
I personally have not dealt with that. However, if you are returning results of a query from within a method, it would be best to throw your results into an ArrayList and return that. The mere fact that a ResultSet is being returned from a method would tell me that a keep-alive for the result is not neccessary. Besides, I think that, unless there is a definite need to maintain the ResultSet, the data should ALWAYS be put in an ArrayList (or Vector). Otherwise it is unneccessary DB server labor.
Suppose if you close the connection object or if the connection object is a method level variable and pass only the resultset to the main method, then null object is returned. So it would be better to put it in collection object and use it for further processing
Welcome to the Ranch "Ranch Bingo"! You'll find this forum a great place to seek help on JDBC, and there aren't many rules you'll have to worry about, but one is that proper names are required. Please take a look at the JavaRanch Naming Policy and change your display name to match it. In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious. Thanks! bear Friendly Neighborhood Bartender [ February 20, 2004: Message edited by: Bear Bibeault ]