?
Solved

.Net and SQL Database connections

Posted on 2014-09-12
4
Medium Priority
?
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 1500 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 84

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

764 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