Solved

.Net and SQL Database connections

Posted on 2014-09-12
4
178 Views
Last Modified: 2014-09-18
General question about .Net and SQL Database connections; When to open and close connection?

General program logic flow is -

Open DB connection
get data
Close DB connection

perform some logic

Open DB connection
get data
Close DB connection

perform some logic

repeat above necessary.

Should the DB connection be closed after each get data as in the example, or should we open the connection once, and close it at the end of the program i.e.

Open DB connection
get data

perform some logic

get data

perform some logic

get data

perform some logic

Close DB connection.

Thanks for your help.
0
Comment
Question by:ordo
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 40319687
If the data is logic dependant  (eg: first set drives second set) I would open and close the connection each time.



If all the data can be grabbed at once I would grab both sets in one connection and then perform logic on each set as needed.
0
 

Author Comment

by:ordo
ID: 40320312
Kyle,

Thanks for your response. The data 'get's are logic dependent, so open and close each time seems to be the way to go. The problem we're running into is that we get Max Connection (max of 200) errors when the webpage is getting heavy use.

Do you know if the connection is returned to the pool immediately upon closing, or is there some action we can take to facilitate that?

That being said, I am going to review the logic and code to see if we can't reduce the number of connections.

Best regards,

Pat
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40320616
Your question is in the MySQL Topic Area.  If you are using the MySQL .NET connector in your application, you will find that it does some of the connection management itself.  http://dev.mysql.com/downloads/connector/net/
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40323255
How many users?  It could be that you're just using up that many connections.

If you call the conn.Close() it should release that connection.

This should be easy enough to do on your local machine:

Point the .net code at a test server or on your local machine.  The idea being that you're going to be the only one connecting to it.

Set a break point after the open, after the close, and after the open again.

Right before you run the program (to get a baseline), and at each break point, run this to check the number of connections:

SHOW STATUS WHERE `variable_name` = 'Threads_connected'
     - or  - 
SHOW PROCESSLIST

Open in new window


You should see the number of connections being affected.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question