• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What's the fastest way to free a ResultSet?

 
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's a dangling reference?
 
graham king
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic