• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not closing ResultSet and PreparedSatement in Finally block?

 
Krish Yeruva
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,
I have one method which is getting connected to DB. And executing the query using prepared Statement. But the thing is ResultSet(rs) and PreparedStatement(ps) are getting closed in try block only(not in Finally). So here my question is:
What will happen if we don't close rs and ps in Finally block?
Is there any performance hit on the application if we don't close rs and ps in Finally block?

Thanks in advance

Regards,
Krishna
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if some code throws an exception before the resources are closed?
 
Krish Yeruva
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bibeault,
Thank you so much for your quick response.
The method is having only one query, which never returns neither null nor empty. (I have check that table in DB. SO the all the columns are having the values).
So in this case, is there any performance hit on the web application if I closed RS and PS in the try block?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't answered my question.
 
Krish Yeruva
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Answer for your question is, In my thoughts that resources keep open in the pool memory
Please correct me if i am wrong.

In previous reply, what i am trying to say is there is no possibility of throwing an exception before the resource are closing because it is simple query.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Krishnareddy Yeruva wrote:there is no possibility of throwing an exception before the resource are closing because it is simple query.

So sure are you?
 
Krish Yeruva
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bibeault
Yes I am very sure about it.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's an incorrect answer, unfortunately. Exceptions can and will occur, even with the simplest queries. There is a lot of exceptions that can occur - a network glitch, the database running out of allocated disk space, or some admin actions on the database may all result into exceptions being thrown.

I'd say that not closing the resources in a finally block is a bug. It's not a performance question at all. It is much, much easier to fix that bug than to decide whether that bug in this particular instance may or may not cause serious problems in your database or application.
 
Krish Yeruva
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Martin,
" Exceptions can and will occur, even with the simplest queries. There is a lot of exceptions that can occur - a network glitch, the database running out of allocated disk space, or some admin actions on the database may all
result into exceptions being thrown."

What you are saying is cent% correct. In that case even if we close the rs/ps inside finally block doesn't execute. right. Just to know, how it will make difference between closing of rs/ps inside try block or finally block in these cases?
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the difference is that if an exception occurs - for whatever reason - while executing the SQL statement itself, the rest of the try block won't execute, but the finally block will. So if you put your closing statements into the finally block, they will be called. You still need to handle exceptions raised in the finally block correctly, so that if, for example, there is an exception closing the resultset, you'll catch it and close the prepared statement as well.

It can be a lot of code in Java 6 or earlier, but much less in Java 7. The proper way to close resources was recently discussed here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic