473,396 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

.NET -> Database best practices

Plater
7,872 Expert 4TB
I don't think I brought this up before, so I'm going to ask it.

My recent style of using a database in my applications (web-based and windows-based) has been to create a static class called "DBFunctions"
with functions like
Expand|Select|Wrap|Line Numbers
  1. public static DataTable GetAllUsers()
  2. {/**/}
  3.  
Now, I use ONE dbconnection for everything. At the begining of each public db function I have a helper function that checks the state of the dbconnection object and waits for it to become in either the open or closed state (as opposed to a transitional state) and then procedes to make sure it's closed, and then re-opens it.

Is closing and re-opening the connection a bad practice? I remember learning that creating a new connection to database is "costly" but that modern db server implentations actually have the connections created already in a pool and your objects just pick one from the pool, thus removing the cost of opening and closing.
Is this true? Or should I put some more thought into the best practice for database usage?
Sep 18 '07 #1
1 1291
davef
98
I don't think I brought this up before, so I'm going to ask it.

My recent style of using a database in my applications (web-based and windows-based) has been to create a static class called "DBFunctions"
with functions like
Expand|Select|Wrap|Line Numbers
  1. public static DataTable GetAllUsers()
  2. {/**/}
  3.  
Now, I use ONE dbconnection for everything. At the begining of each public db function I have a helper function that checks the state of the dbconnection object and waits for it to become in either the open or closed state (as opposed to a transitional state) and then procedes to make sure it's closed, and then re-opens it.

Is closing and re-opening the connection a bad practice? I remember learning that creating a new connection to database is "costly" but that modern db server implentations actually have the connections created already in a pool and your objects just pick one from the pool, thus removing the cost of opening and closing.
Is this true? Or should I put some more thought into the best practice for database usage?
Connection pooling feature is intrinsic to the Connection object shipped with ADO.NET 2 and is enabled by default. Unless you explicitly disable it in the connection string, you seem to be inadvertently using it even with a singleton connection object. It's considered a good practice to keep connection pooling enabled to boost performance savings on connection opening-closing overhead. I believe there's a way to specify the max size of the pool, too.
You can benchmark performance gains with a code similar to this:
Expand|Select|Wrap|Line Numbers
  1. SqlConnection testConnection =  new SqlConnection(@"Data Source=(local)\SQLEXPRESS;Initial Catalog=DEMO;Integrated Security=SSPI;");
  2. long startTicks = DateTime.Now.Ticks;
  3. for (int i = 1; i <= 100; i++)
  4. {
  5.     testConnection.Open();
  6.     testConnection.Close();
  7. }
  8. long endTicks = DateTime.Now.Ticks;
  9. Console.WriteLine("Time taken : " + (endTicks - startTicks) + " ticks.");
  10. testConnection.Dispose();
  11.  
To disable connection pooling, put "Pooling=false" in connection string.
This code snippet is from
http://www.c-sharpcorner.com/UploadF...nnPooling.aspx
Sep 18 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: byrocat | last post by:
I'm chasing after a documetn that was available on one of the Microsoft websites that was titled somethign like "MS SQL Server Best Practices" and detailed a nyumber of best practices about...
1
by: ===Steve L.=== | last post by:
I'm new to vb studio .net and sql2k, and wondered if any one know any good articles on the subject? such as the best practices and configuration for the database connection and security model....
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
0
by: David Helgason | last post by:
I think those best practices threads are a treat to follow (might even consider archiving some of them in a sort of best-practices faq), so here's one more. In coding an game asset server I want...
0
by: Louis Aslett | last post by:
I hope this is the correct newsgroup for this query (if not please give me a pointer to where is best): I understand the theory of normalisation etc and am trying to follow best practices in the...
4
by: Collin Peters | last post by:
I have searched the Internet... but haven't found much relating to this. I am wondering on what the best practices are for migrating a developmemnt database to a release database. Here is the...
6
by: Nate | last post by:
I am in a slight predicament trying to determine the most efficient and effective way to connect/disconnect from a database within a business object (c# dll). I'm also keeping in mind the concept...
4
by: DeepDiver | last post by:
I am developing an inventory database in SQL Server. I realize there are many commercial (as well as some non-commercial) inventory offerings, but my client has specific requirements that would...
9
by: Yehia A.Salam | last post by:
Hello, I am building an ASP.net website that connects to an XML database, and was wondering which is the best way to create the connection if I need frequent access to the database, I have one...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.