• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

closing connection?

 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34208
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
N.Senthil Kumar
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jan Cumps
Bartender
Posts: 2586
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
N.Senthil Kumar
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you , Guys...
 
abhi vazhakalil
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic