aspose file tools*
The moose likes JDBC and the fly likes Using Resultset object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Using Resultset object" Watch "Using Resultset object" New topic
Author

Using Resultset object

Sahil Sharma
Ranch Hand

Joined: Aug 27, 2003
Posts: 152
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
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
>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

Joined: Aug 27, 2003
Posts: 152
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 ?
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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?


There is no emoticon for what I am feeling!
Adam Richards
Ranch Hand

Joined: Nov 03, 2005
Posts: 135
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.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30506
    
150

Originally posted by Ilja Preuss:
Why?

Why not?

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


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30506
    
150

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Resultset object