aspose file tools*
The moose likes JDBC and the fly likes closing connection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "closing connection?" Watch "closing connection?" New topic
Author

closing connection?

sahar sa
Ranch Hand

Joined: Jul 06, 2009
Posts: 102
Dear friends,
I have a problem with making and closing the connection to my data base.
let me explain, I closed my CONNECTION and PSTMT after insert query but when I run this code after the first round I get the below error :
code:
trigger code:



insert code:


error:


so according to this error I should remove closing conn and pstmt but in all the examples I found they closed them.

Can some one tell me what will happen if i do not close my CONNECTION and PSTMT after insert, delete or select?

thnx,
Sahar.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

If you do not close them the resources remain in use and quickly you will run out of available connections to your database. When you open a connection you should always close it in a finally block.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
sahar sa
Ranch Hand

Joined: Jul 06, 2009
Posts: 102
Dear,
Do I need to close it in a finally block? Because I usually call this MySQL queries from another class, so it reurns me a result set (after I will proceed on result set some) and then I close it by calling DB.close().


Am I doing It wrongly? why I need to use finally block?
thank you, in advance.
Sahar
Srikanth Nalam
Greenhorn

Joined: Feb 23, 2010
Posts: 20
hello sahar,

I didn't understand the problem completely. But in your recent post i observed that you are using "else if" for closing connection. So it will be closed only if pstmt is null.

Ignore this if you are already aware of this.


Thanks & Regards,
Srikanth Nalam.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30758
    
156

Sahar,
The whole closeDB() method can be called from a finally block. But yes, you do need a finally block to ensure the resources are closed.


[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
N.Senthil Kumar
Ranch Hand

Joined: Jul 07, 2009
Posts: 50
Paul Sturrock wrote:If you do not close them the resources remain in use and quickly you will run out of available connections to your database. When you open a connection you should always close it in a finally block.



Hi ,

May i know , do we get any specific exception if the Connection are not closed or if we run out of available connections of our database, what will happen. ?


Thanks in advance.


N.Senthil Kumar
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2503
    
    8

Hi,

I googled "what happens if a database runs out of connections", and found out that lots of bad things can happen.
You are waisting database resources. It depends on your database flavor (and lots of other things) how it deals with the situation.
Your program may slow down, block, get exceptions...
Regards, Jan


OCUP UML fundamental and ITIL foundation
youtube channel
N.Senthil Kumar
Ranch Hand

Joined: Jul 07, 2009
Posts: 50
Thank you , Guys...
abhi vazhakalil
Greenhorn

Joined: Mar 24, 2010
Posts: 4
sahar sa wrote:Dear friends,
I have a problem with making and closing the connection to my data base.
let me explain, I closed my CONNECTION and PSTMT after insert query but when I run this code after the first round I get the below error :
code:
trigger code:



insert code:


error:


so according to this error I should remove closing conn and pstmt but in all the examples I found they closed them.

Can some one tell me what will happen if i do not close my CONNECTION and PSTMT after insert, delete or select?

thnx,
Sahar.



I executed this above program successfully without any exception. As whenever you are calling the insert method, you are creating a fresh connection object. So if you close connection, it does'nt matter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: closing connection?