• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

closing connection?

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you , Guys...
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic