When to close a db connection?

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 19
Reputation: mcgarry101 is an unknown quantity at this point 
Solved Threads: 0
mcgarry101 mcgarry101 is offline Offline
Newbie Poster

When to close a db connection?

 
0
  #1
Dec 3rd, 2008
Hi,

just a quick question from a newbie! I have recently inherited an asp.net website from an ex-colleague and I have a question regarding db connections. I am using vb.net for code behind with an oracle 9 database. The site at the moment, sets up a database connection GDBConn in the session_start sub found in global.asax.vb. This is then used throughout the site. It then doesnt close it until the session_end. However, I was wondering if anyone could tell me if it would be better to be closing these connections immediately after using them. For example, I have a fillGridView function I use to fill my gridviews. In the finally section of this should I be closing my db connection? I am already calling

  1. DbComm.Parameters.Clear()
  2. DbComm.Dispose()
  3. DbRead.Close()

but should I be closing the connection too?
Any help would be really appreciated.

Thanks,

David.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 268
Reputation: Traicey is an unknown quantity at this point 
Solved Threads: 19
Traicey's Avatar
Traicey Traicey is offline Offline
Posting Whiz in Training

Re: When to close a db connection?

 
0
  #2
Dec 4th, 2008
Yah sure if u open the connection u surely need to close it and the best way to close connection is to close it in a finally statement if u have try/catch statement or just after using it, it is not a good practise to leave the connection open and it also bogs down the DB Server, so close it whenever u opened it
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 101
Reputation: PirateTUX is an unknown quantity at this point 
Solved Threads: 3
PirateTUX's Avatar
PirateTUX PirateTUX is online now Online
Junior Poster

Re: When to close a db connection?

 
0
  #3
Dec 4th, 2008
I concur... I always close my DB connections right after I'm done with whatever group of transactions I needed the connection for. It makes it easier for me to remember when things are cleaned up, and doesn't leave any connections dangling opened.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 645
Reputation: binoj_daniel is an unknown quantity at this point 
Solved Threads: 17
binoj_daniel's Avatar
binoj_daniel binoj_daniel is offline Offline
DaniWeb Expert

Re: When to close a db connection?

 
0
  #4
Dec 16th, 2008
Placing all the DB code inside the Using keyword takes care of all the resource issues automatically.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC