Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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

Using Resultset object

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Can i use resultset object after the connection and statement objects are closed. I am using Java/Mysql.

Currently I am getting an error when i try to use my resultset object after closing my connection and statement

Error: java.sql.SQLException: Operation not allowed after ResultSet closed
~thnx
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>Can i use resultset object after the connection and statement objects are closed?
No.

If you close a statement it closes all its child result sets.
If you close a connection it closes all its child statements (and thus the result sets)

However good code should not rely on this. You should explicitly close all of your result sets, statements and connections (in that order)
 
Sahil Sharma
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when do we get Too Many Connections exception ?.
When connection objects are not closed ?
Do we also get this error when dont close resultsets objects explicitly ?
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sounds like something that would be useful for you to solve yourself:

1. Write a loop that opens lots of connections.
To keep them from being garbage collected, put them
in an array or Collection.

2. Write a loop that: opens a connection, creates a statement
and executes a simple query, like SELECT * FROM sometable,
and stores the ResultSet in an array or Collection. Then
close the Connection.

What is your conclusion?
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sounds like a bug. I'd look for connections being continually opened without old ones being closed. My own personal style is that once I open a connection, I keep it open as long as possible, until I'm done using it, then explicitlyi close it.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Stefan Evans:

However good code should not rely on this. You should explicitly close all of your result sets, statements and connections (in that order)



Why?

Anyway, moving to JDBC forum...
 
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
Why?


Why not?

Also, are you questioning the need to close these resources or the ordering?
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jeanne Boyarsky:
Why not?



Because I don't like doing redundant work.


Also, are you questioning the need to close these resources or the ordering?



I question that you should close a ResultSet when you are closing the corresponding Statement anyway. It just seems to make the code so much more complicated (especially if you try to get the exception handling right), without any benefit.
 
Jeanne Boyarsky
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ilja,
Well yes. But if you have a JDBC framework to run queries this code is only written once. And it seems good to have on the off chance that you are using an old driver that doesn't give all the resources back.
 
ice is for people that are not already cool. Chill with 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