Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

What's the fastest way to free a ResultSet?

 
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!
 
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!
 
Marshal
Posts: 25676
69
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.
 
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
Marshal
Posts: 25676
69
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?
 
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 ]
 
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!
 
You’ll find me in my office. I’ll probably be drinking. And reading this tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic