File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Can ResultSet be reliably returned from method that creates Stmt and executes query? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Can ResultSet be reliably returned from method that creates Stmt and executes query?" Watch "Can ResultSet be reliably returned from method that creates Stmt and executes query?" New topic
Author

Can ResultSet be reliably returned from method that creates Stmt and executes query?

Marshall B Thompson
Ranch Hand

Joined: Apr 11, 2002
Posts: 42
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
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
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.

Just a thought!


An egg is a chicken's house!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

I never pass resultsets around.
the data should ALWAYS be put in an ArrayList (or Vector)

or whatever other value objects that make sense. Doesn't have to be a list or vector if that doesn't suit the nature of the data.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jason Steele
Ranch Hand

Joined: Apr 25, 2003
Posts: 100
True Bear! Tryin to keep it simple.
Mani Narayanan
Greenhorn

Joined: Feb 20, 2004
Posts: 5
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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

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 ]
 
 
subject: Can ResultSet be reliably returned from method that creates Stmt and executes query?