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 What's the fastest way to free a  ResultSet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "What Watch "What New topic
Author

What's the fastest way to free a ResultSet?

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
I was wondering if there's any other way than closing the resultset... I'm asking this because I need to use the ResultSet immediately(make another query) after query... Thanks!


SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
Rajasekar Elango
Ranch Hand

Joined: Sep 13, 2004
Posts: 105
store query result to List or some other data structure..

Thanks,
Raja.


SCJP 1.4
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Yes, that's what I'm doing... What I want to know is if it's ok to do this...


rs = statement.executeQuery(query);

rs.close();

rs = statement.executeQuery(anotherQuery);

rs.close();

Thanks!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Yes. And your question about "reusing the ResultSet" is misguided. That code creates two ResultSet objects, it does not create one and then reuse it. What IS reused is the variable that refers to those objects.
graham king
Ranch Hand

Joined: Dec 30, 2004
Posts: 133
Hey Paul,

I've got a similar situation with a PreparedStatement...



Will the single AppHelper.closeStatment(ps); leave a dangling reference?

Thanks


write, revise, re-write, revise, again...<br />write, revise, re-write, revise, again...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

What's a dangling reference?
graham king
Ranch Hand

Joined: Dec 30, 2004
Posts: 133
What I was trying to say, is if I only use the closeStatement() once, then, will the initial preparedStatement still be '?active?' in memory?
Manhar Puri
Ranch Hand

Joined: Aug 23, 2005
Posts: 41
yes the the initial preparedStatement object will still be there in memory, but there are no dalgling reference (or what I think you mean is dangling pointer) in java.

Since the initial preparedStatement object is not being referenced by any variable it will be garbage collected (I suppose).

-Manhar.
[ June 15, 2006: Message edited by: Manhar Puri ]
stu derby
Ranch Hand

Joined: Dec 15, 2005
Posts: 333
Yes, that code leaves an unclosed, unreferenced PreparedStatement.

If your driver truly complies with the JDBC spec, it will be closed when the garbage collector collects it. Also, if your driver is truly JDBC spec compliant, then if the connection is truly closed (as opposed to being returned to a connection pool), then the PreparedStatement will also be closed when the connection is closed.

You should probably explicitly close it, rather than relying on the "magic" of JDBC. In particular, this code might work well for a long time, then suddenly break when connection pooling is introduced or your garbage collection frequency changes. Similarly, it might pass all unit tests and fail in a production environment.
graham king
Ranch Hand

Joined: Dec 30, 2004
Posts: 133
Originally posted by stu derby:

You should probably explicitly close it, rather than relying on the "magic" of JDBC. In particular, this code might work well for a long time, then suddenly break when connection pooling is introduced or your garbage collection frequency changes. Similarly, it might pass all unit tests and fail in a production environment.


Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What's the fastest way to free a ResultSet?