Solved

.Net and SQL Database connections

Posted on 2014-09-12
4
176 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 82

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
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 …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now