• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What's the fastest way to free a ResultSet?

 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Rajasekar Elango
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
store query result to List or some other data structure..

Thanks,
Raja.
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21583
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Paul,

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



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

Thanks
 
Paul Clapham
Sheriff
Posts: 21583
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's a dangling reference?
 
graham king
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic