Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Resultset object

 
Sahil Sharma
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
 
Stefan Evans
Bartender
Posts: 1784
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 ?
 
Jeff Albertson
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?
 
Adam Richards
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.
 
Ilja Preuss
author
Sheriff
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...
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • 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
Sheriff
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
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic