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.
This message was edited 1 time. Last update was at by Ulf Dittmer
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.
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
posted
0
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.
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. ?
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
Joined: Jul 07, 2009
Posts: 50
posted
0
Thank you , Guys...
abhi vazhakalil
Greenhorn
Joined: Mar 24, 2010
Posts: 4
posted
0
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.