Wrox Programmer Forums
|
ASP.NET 3.5 Professionals If you are an experienced ASP.NET programmer, this is the forum for your 3.5 questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Professionals section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 12th, 2008, 09:20 AM
Authorized User
 
Join Date: Jun 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default Data Access Layer Question

This is an architecture question. Which is a better method for database access?

1. Open and close the connection with each Data Access Layer method call?

2. Have a connection object at the page level that only gets opened once and is used for all Data Access Layer method calls during that page life cycle, and dispose of it in the page_unload?
 
Old December 12th, 2008, 09:52 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

As a general rule of thumb you should open your connection, process your request, and then close your connection directly after. By doing this it is less likely that you are going to run into connection leaks and other unsavory things that can grind your application to a halt.

However, there is nothing wrong with this code:

CSharp Code:
public static class DataAccess
{
   private static SqlConnection con;
   private static SqlCommand cmd;

   private static DataRowCollection DoSomething()
   {
         //Open connection
         //Process
         //close connection
         //return
   }
}

Also, if you are using SQL Server you may want to look into using the Enterprise Libraries for data access: http://www.codeplex.com/entlib

hth.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
 
Old December 13th, 2008, 12:50 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

I'm a big fan of nested using statments to implement your option 1 for each of your Select, Insert, Update, Delete, etc. methods. Like:

Code:
public static SomeList GetSomeList()
{
 ... 
 using (SqlConnection connection = new SqlConnection(AppConfiguration.ConnectionString))
            {
                connection.Open();
                using (SqlCommand command = connection.CreateCommand())
                {
                    // Initialize command
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        /// Load collection
                    }
                }
            }
            return collection;
}
etc., etc....

That way your IDisposable objects (like your connection object) will be disposed automatically when the using statment exits.

Using statemens actually compile to:

Code:
try
  {
    SqlConnection connection = new SqlConnection(AppConfiguration.ConnectionString)
    connection.Open();
  }
  finally
  {
    if (connection != null)
      ((IDisposable)connection).Dispose();
  }
So connection.Dispose gets called even if an exception occurs. Module level connection objects are old school. Check out MSDN on using statments.

HTH,

Bob
 
Old December 13th, 2008, 01:32 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Oh, and check out Imar's N-Layered Web App articles at http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=476 if you havn't already. He also takes the nested using statement approach, as does CSLA, as do many methods in the SqlHelper class in the older version of the Data Access Application Block (havn't checked out the new version in Enterprise Library 4.1 that Doug mentioned yet), as does probably every other high profile data access library you're likely to run into out there. I think its pretty fair to say that using statement syntax is a prevailing data access layer "best practice".
 
Old December 16th, 2008, 05:49 AM
Wrox Author
 
Join Date: Jul 2008
Posts: 74
Thanks: 1
Thanked 8 Times in 8 Posts
Send a message via MSN to msellers
Default

I am another big believer in using the APplication Blocks as it really simplifies the management on the application side, but the key is as others have mentioned to ensure that the connections are always closed. Also, I've found that keeping the connections open for as little time as possible is a good thing, especially as applications begin to grow in size and load.
__________________
Mitchel Sellers
Microsoft C# MVP, MCITP
Director of Development
IowaComputerGurus Inc.

My blog for .NET and DotNetNuke info

Author of "Professional DotNetNuke Module Programming"

Tech Editor on "Visual Studio 2010 six-in-one" and "Pro C# 4.0"





Similar Threads
Thread Thread Starter Forum Replies Last Post
Handle error from database layer to applican layer khatu_jec ASP.NET 2.0 Basics 1 November 9th, 2008 03:51 PM
Independent Data Access Layer Sumith Asanka .NET Framework 2.0 2 September 28th, 2007 02:20 AM
Data Abstraction Layer kcraft Pro PHP 6 March 18th, 2006 04:29 PM
Question about Business Layer hasanali00 BOOK: ASP.NET Website Programming Problem-Design-Solution 3 March 21st, 2005 06:49 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.