Re: Global Database connection in all classes

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Karl Seguin (_at_)
Date: 10/04/04


Date: Mon, 4 Oct 2004 14:30:45 -0400

Bryan,
>From what I understand you are trying to do, best practice goes against it
;) You seem to be trying to set it up so that a single connection is opened
throughout the life of a request. With ADO.Net and connection pooling, the
prefered method is to keep connections closed for the shortest time possible
/ the smallest unit. It's far better to create new database connections and
open them on a as-needed basis.

For example, it's much better to do this:

function login_click
  Call UserIsLoggedIn
   if true then
     Call GetUserInfo
  end if
end funciton

function UserIsLoggedIn
  open connection
  validate user
  close connection
end function

function GetUserInfo
  open connection
  get user info
  close cnnection
end function

than to do this:

function login_click
  open conection
  Call UserIsLoggedIn
  Call GetUserInfo
  close connection
end funciton

function UserIsLoggedIn
  validate user
end function

function GetUserInfo
  get user info
end function

Granted that's a pretty trivial example...

If my word isn't good enough for you...From Scott Gu
(http://scottgu.com/PerformanceEurope.zip)

Code Recommendation:
"Open connections in your code late, and then close them early"
Don't hold on to connections for long periods of time - do not try to build
your own "smart" connection pool logic
Close the connection as soon as you are finished with it (this returns it to
the pool)

-- 
MY ASP.Net tutorials
http://www.openmymind.net/
"Bryan" <bryanbabula@yahoo.com> wrote in message
news:9ef3ebb0.0410040739.5e2c6ed3@posting.google.com...
> Hello,
> I'm just starting to develop in asp.net and i have a question about
> using a database connection globally in my app. I have set up the
> procedures for getting all my connection string info which each page
> will use, but my question relates to how to use the database
> connection i create in all my classes.
>
> I have a database class, in a separate namespace and file, i created
> that handles all the connection opening, executing statements etc. I
> also use 2 other classes in my asp.net app. which are in the same
> namespace and same file.
> 1 holds information specific to the current user logged in, and the
> other class is used as a global functions class. I want to be able to
> open the db connection once, and no matter if i'm running a method in
> the UserSession class, or the Global Functions class to be able to use
> that same Database class i created, without creating a new instance of
> the database class in each class i want to run this in.
>
> example. my UserSession class might have the web login authentication
> method in it that gets run when the user clicks the login button on
> the login page, right after that method runs, which again is in the
> user session class, i might run a method from the global functions
> class that needs to use the database. i wanted to use the same
> Database class i instantiated instead of creating a separate instance
> of the database class for each class i needed it in. Can this be done?
> or do i need to change my thinking around a little and use a different
> model.
>
> thanks for any help you can offer. Any links to any "best practice"
> type info/articles would be appreciated as well.
> Bryan


Relevant Pages

  • Re: Global Database connection in all classes
    ... "Bryan" wrote in message ... > using a database connection globally in my app. ... > I have a database class, in a separate namespace and file, i created ... > other class is used as a global functions class. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: [PHP] class/object design
    ... > instantiate one object for the entire page and let any method in that ... By extending the database class you tightly couple your product class ... instantiating an instance of the database connection everytime you want ...
    (php.general)
  • Re: (Mis)use of transactions
    ... Is it a UI class or a database class? ... And quite possibly the connection times out causing all of the users data to be discarded. ... You can do that relatively efficiently by putting triggers on the main table that copy updates into shadow tables. ...
    (comp.lang.java.databases)
  • Re: Where to place app.config file
    ... connection string into your Database class in its contructor. ... DataLayer to be re-used by other apps that pass in different connection ...
    (microsoft.public.dotnet.languages.csharp)
  • Conn.Close() and Login Error w/Subsequent Conn.Open()
    ... I have a data conversion program that is reading data by accessing the Sql ... The exact same connection string is used for all ... Open Connection ... Execute several Inserts via ExecuteNonQuery ...
    (microsoft.public.dotnet.framework.adonet)