Solved

.Net and SQL Database connections

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

23 Experts available now in Live!

Get 1:1 Help Now